About creating a service node

1. Preparation of environmental conditions:

  A. Determining node has been created

  B.npm or cnpm have downloaded, npm and cnpm actually a reason

  C.mysql or use other databases installed (in this case mysql)

 

2. Begin to create, first create a new folder, assuming the name for the serv

 

3. Open cmd corresponding path, and enter npm init -y package.json initialization (default represents -Y press enter, if not write line, only the configuration becomes package.json manual configuration),

This time next serv folder will generate a file package.json

 

4. Install mysql-dependent, the input npm install mysql --save, it will own creation node_modules folder and its dependencies (mysql is just one example, you can install other dependent, can also be created node_modules folder and its dependencies), a package -lock.json file

 The same dependence downloads: node comprising middleware framework connect (or express other like dependencies) to create a connection, body-parser (body parsing), mysql, util (url.parse method of resolution parameters in the URL) and the like, wherein, due to inconsistencies node version, you may need to rely on more than just the case of (the current node version 10.16.0);

 

5. In the creation of a folder serv js file name can be easily taken, but the best reference to a named server based file server.js the present example, the following sample code:

var connect = require('connect'); //创建连接
var bodyParser = require('body-parser'); //body解析
var util = require('util'); //url.parse 方法来解析 URL 中的参数
var mysql = require('mysql'); //mysql
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '123456',
database: 'glass_min'
});

var app = connect();
app.use(bodyParser.json()) //JSON解析
.use(bodyParser.urlencoded({
extended: true
}))
//use()方法还有一个可选的路径字符串,对传入请求的URL的开始匹配。
//use方法来维护一个中间件队列
.use(function(req, res, next) {
//跨域处理
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*'); //允许任何源
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); //允许任何方法
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,X-Session-Token'); //允许任何类型
res.writeHead(200, {
"Content-Type": "text/plain;charset=utf-8"
}); //utf-8转码
next(); //next 方法就是一个递归调用
})
//通用通用设备列表
.use('/v1/cnc/common/mac_list', function(req, res, next) {
console.log("[通用设备列表]:");
console.log(req.body);
var result = {
"code": 200,
"msg": "success",
"data": []
}
var sql = 'SELECT code,id,name FROM `equlistComm` WHERE id = "aaf6387e-9607-41ff-b2b4-93b35fcbc795"';
conQueryData(sql, result, res, next());
})
.listen(3001);
console.log('Server started on port 3001.');

/**
* sql操作
* @param {Object} sql
*/
function conQueryData(sql, result, res, fun) {
connection.query(sql, function(error, results, fields) {
if(error) throw error;
result.data = results;
res.end(JSON.stringify(result));
fun;
});
}

 

6.在cmd上运行node+文件全称,当前示例为:node server.js

 

Guess you like

Origin www.cnblogs.com/Chansea/p/node-server.html