node 基本搭建 server.js
Posted 逻辑短路
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了node 基本搭建 server.js相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章