博客
关于我
node.js手写http服务器连接mysql数据库
阅读量:241 次
发布时间:2019-03-01

本文共 5239 字,大约阅读时间需要 17 分钟。

代码

数据库数据:

创建数据库:

CREATE DATABASE zfw;

选择数据库:

USE zfw;

创建数据表:

CREATE TABLE `zfw_fangattrs`  (  `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,  `pid` int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '上级ID',  `field_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '字段名',  `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '属性名称',  `icon` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '图标',  `created_at` timestamp(0) NULL DEFAULT NULL,  `updated_at` timestamp(0) NULL DEFAULT NULL,  `deleted_at` timestamp(0) NULL DEFAULT NULL,  PRIMARY KEY (`id`) USING BTREE) ENGINE = InnoDB AUTO_INCREMENT = 24 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;

数据表插入数据:

INSERT INTO `zfw_fangattrs` VALUES (7, 0, 'fang_rent_type', '付款方式', '/uploads/fangattr/RSyZirp29d9EJU0yMxJ8MXEVn6adosbzv3LewVVw.png', '2020-06-09 20:04:32', '2020-06-09 21:32:36', NULL);INSERT INTO `zfw_fangattrs` VALUES (8, 7, '', '微信支付', '/uploads/fangattr/DyGLg4qC5xUGI9rTwYj9hxw7AsXlp5X0gNDzTOHl.png', '2020-06-09 20:05:57', '2020-06-09 21:32:43', NULL);INSERT INTO `zfw_fangattrs` VALUES (9, 7, '', '支付宝支付', '/uploads/fangattr/gd6fBNRHac8NJ1kvap7kwYTnWMqqCC1WldRyX4JM.png', '2020-06-09 20:06:33', '2020-06-09 21:32:51', NULL);INSERT INTO `zfw_fangattrs` VALUES (10, 7, '', '现金支付', '/uploads/fangattr/dkMyr72vOMGbdIV2eVp9Ncuaz8peJecOuB0NxqKY.png', '2020-06-09 20:06:58', '2020-06-09 21:38:12', NULL);INSERT INTO `zfw_fangattrs` VALUES (11, 0, 'fang_dirrection', '房屋朝向', '/uploads/fangattr/72r2tktHJlIaKDYXM8BbM3VD4EpgKrVghzsWHwEz.png', '2020-06-09 21:44:17', '2020-06-09 21:44:17', NULL);INSERT INTO `zfw_fangattrs` VALUES (12, 11, '', '坐北朝南', '/uploads/fangattr/2ex8pk7DnZJ4Vvn7xtL6C2hb59MkpX5PhdZpW9I7.png', '2020-06-09 21:45:26', '2020-06-09 21:45:26', NULL);INSERT INTO `zfw_fangattrs` VALUES (13, 11, '', '坐南朝北', '/uploads/fangattr/IvLBEzeJDz2A7a9dPhA9XYgU9abZycl5pd6yCzlK.png', '2020-06-09 21:46:21', '2020-06-09 21:46:21', NULL);INSERT INTO `zfw_fangattrs` VALUES (14, 0, 'fang_rent_class', '租凭方式', '/uploads/fangattr/CqAeXs4Tj8zpgpQ7kXrM8jB7n4f99QxOA9WYD0XV.png', '2020-06-09 21:52:21', '2020-06-09 21:52:21', NULL);INSERT INTO `zfw_fangattrs` VALUES (15, 14, '', '付一押一', '/uploads/fangattr/tRHWto2Jkil8ElfKw7fCEELpw9soaUbNwub6U4uX.png', '2020-06-09 21:52:38', '2020-06-09 21:52:38', NULL);INSERT INTO `zfw_fangattrs` VALUES (16, 14, '', '付一押三', '/uploads/fangattr/mJTRRfO1szrceHxB3D8MLWKxlVSrF9tB3mqAEcy0.png', '2020-06-09 21:52:59', '2020-06-09 21:52:59', NULL);INSERT INTO `zfw_fangattrs` VALUES (17, 0, 'fang_config', '配套设施', '/uploads/fangattr/cFsspcLZgolCnqufenEHKX2NcJHBiwC0Z4vS4kcL.png', '2020-06-09 21:54:49', '2020-06-09 21:54:49', NULL);INSERT INTO `zfw_fangattrs` VALUES (18, 17, '', '热水器', '/uploads/fangattr/wb0T77tNMiWuJN7l6KEvkPqdVmj86HBkAkjKdqsw.png', '2020-06-09 21:55:14', '2020-06-09 21:55:14', NULL);INSERT INTO `zfw_fangattrs` VALUES (19, 17, '', '宽带', '/uploads/fangattr/G0pursKORZ4IO0LiCUUW1Hcfi08j1KQsEIBUfs4x.png', '2020-06-09 21:55:29', '2020-06-09 21:55:29', NULL);INSERT INTO `zfw_fangattrs` VALUES (20, 17, '', 'wifi', '/uploads/fangattr/51bBLVkFTTz0UwIyE2t9G7pqv7Wfj2O9cJ6ieyRs.png', '2020-06-09 21:55:57', '2020-06-09 21:56:15', NULL);INSERT INTO `zfw_fangattrs` VALUES (21, 17, '', '洗衣机', '/uploads/fangattr/i7B2b0I4oZjTkZcNQ9PXVgwhzoxq1CrLEbdY3O7M.png', '2020-06-09 21:56:32', '2020-06-09 21:56:32', NULL);INSERT INTO `zfw_fangattrs` VALUES (22, 17, '', '停车场', '/uploads/fangattr/CkOB1Wm9x9NMQOpVxzNDxXqb3abBqhocyVBTx9po.png', '2020-06-09 21:56:51', '2020-06-09 21:56:51', NULL);INSERT INTO `zfw_fangattrs` VALUES (23, 17, '', '空调', '/uploads/fangattr/kVLsgJL5CdALlipsMuApXWZotIEqGDA52OvqcGlC.png', '2020-06-09 21:57:14', '2020-06-09 21:57:14', NULL);

配置node.js连接mysql的环境

配置node.js的环境,将node的源换为阿里源,此步骤自行百度

例子:

node.js代码:

http_server_html.js

const http = require('http');var data = null;http.createServer(function(request, response) {     // 设置响应头  response.writeHeader(200, {       "Content-Type" : "text/html"  });  var mysql = require('mysql');	var connection = mysql.createConnection({   		host: "127.0.0.1",		user: "root",		password: "root",		port: '3306',		database: 'zfw'	});	connection.connect();	connection.query('select * from zfw_fangattrs',function(error, results, fields){   		if(error) throw error;		data = results;	});	connection.end();  // 响应主体//   response.write(JSON.stringify(data));  response.write(`          
node.js 连接 mysql
房源属性表格
ID 上级ID 字段名称 属性名称 图标路径 加入时间
{ { info.id }} { { info.pid }} { { info.field_name }} { { info.name }} { { info.icon }} { { info.created_at }}
`); response.end();})// 设置监听端口为9000.listen(9000);

启动

cmd命令行启动

node http_server_html.js

启动

如果没有报错,就启动成功了,这个窗口就是一个http服务器了。

运行

浏览器访问:http://127.0.0.1:9000/

运行

转载地址:http://erhx.baihongyu.com/

你可能感兴趣的文章
java中的for循环
查看>>
删除外键约束
查看>>
java整体复习
查看>>
public,protect,private
查看>>
C语言指针
查看>>
c++ 预处理命令 #error 用法
查看>>
c++预处理命令#pragma 用法
查看>>
C++ 操作符优先级
查看>>
c++ 关键字
查看>>
C语言文件操作专题
查看>>
C++STL常用拷贝和替换算法
查看>>
QT的QMultiSampleAntiAliasing类的使用
查看>>
c++模板编译
查看>>
C++重载与模板
查看>>
C++树的层次遍历(附完整源码)
查看>>
static关键字的作用?
查看>>
OpenGL 混合Blending
查看>>
OpenGL渲染水water
查看>>
OpenGL fragmentlist片段列表的实例
查看>>
OpenGL hdrb和loom的实例
查看>>