const express = require(‘express‘);
const expressStatic = require(‘express-static‘);
const bodyparser = require(‘body-parser‘);
const multer = require(‘multer‘);
const cookieParser = require(‘cookie-parser‘);
const cookieSession = require(‘cookie-session‘);
const consolidate = require(‘consolidate‘);
const mysql = require(‘mysql‘);
const expressRouter = require(‘express-route‘);
const multerObj = multer({ dest: ‘./static/upload‘ });
const ejs = require(‘ejs‘)
const server = express();
server.listen(8080);
const mysql = require(‘mysql‘);
// 连接池
var db = mysql.createPool({
host: ‘localhost‘,
user: ‘root‘,
password: ‘123456‘,
database: ‘learn‘
})
// 1.请求数据
// get 自带
server.use(multerObj.any());
server.use(bodyparser.urlencoded());
// 2.cookie
server.use(cookieParser());
(function(){
var keys = [];
for (var i = 0; i < 10000; i++) {
keys[i] = ‘keys_‘ + Math.random()
}
server.use(cookieSession({
name: ‘sess_id‘,
keys: keys,
maxAge: 20 * 60 * 1000
}))
})()
// 3.模版
server.engine(‘html‘,consolidate.ejs);
server.set(‘views‘,‘template‘);
server.set(‘view engine‘,‘html‘);
// 4.route
server.use(‘/‘, require(‘./route/web/web.js‘)());
server.use(‘/admin/‘, require(‘./route/admin/index.js‘)());
// 5.default:static
server.use(expressStatic(‘./static/‘))
node 基本搭建 server.js
原文:https://www.cnblogs.com/dekui/p/8981888.html