app.js
var express = require('express');
var path = require('path');
var session = require('express-session')
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var ejs = require('ejs');
var index = require('./routes/index');
var userindex = require('./controler/index');
var debug = require('debug')('exp:server');
var port = normalizePort(process.env.PORT || '4000');
var app = express();
app.set('port', port); var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://127.0.0.1:8080');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Credentials', 'true');
next();
};
app.use(allowCrossDomain);
// view engine setup
app.engine('html', ejs.__express);
// view engine setup
app.set('views', path.join(__dirname, 'public'));
app.set('view engine', 'html'); app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// session 中间件
app.use(session({
name: "mumu", // 设置 cookie 中保存 session id 的字段名称
secret: "mumu", // 通过设置 secret 来计算 hash 值并放在 cookie 中,使产生的 signedCookie 防篡改
resave: false, // 强制更新 session
saveUninitialized: false, // 设置为 false,强制创建一个 session,即使用户未登录
cookie: {
maxAge: 2592000000 // 过期时间,过期后 cookie 中的 session id 自动删除
}
}))
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function (socket) {
console.log("链接成功")
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
app.io=io;
index(app);
userindex(app); // catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
res.render('404');
}); // error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {}; // render the error page
res.status(err.status || 500);
res.render('500');
}); server.listen(port);
server.on('error', onError);
server.on('listening', onListening); function normalizePort(val) {
var port = parseInt(val, 10); if(isNaN(port)) {
// named pipe
return val;
} if(port >= 0) {
// port number
return port;
} return false;
} /**
* Event listener for HTTP server "error" event.
*/ function onError(error) {
if(error.syscall !== 'listen') {
throw error;
} var bind = typeof port === 'string' ?
'Pipe ' + port :
'Port ' + port; // handle specific listen errors with friendly messages
switch(error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
} /**
* Event listener for HTTP server "listening" event.
*/ function onListening() {
var addr = server.address();
var bind = typeof addr === 'string' ?
'pipe ' + addr :
'port ' + addr.port;
debug('Listening on ' + bind);
}
routes/index
app.use('/reg', require('./base/reg')(app.io));
reg.js
const express = require('express')
const router = express.Router()
const regcheck = require('../../sql/reg')
// GET /login 登录页
let reg = function(io) {
router.post('/', function(req, res, next) {
io.emit("reg",{"name":"成功"})
try {
regcheck.checktel(req).then(function(d) {
return regcheck.checknickname(req);
}).then(function(d) {
return regcheck.reg(req)
}).then(function(d) {
res.send(d)
}).catch(function(d) {
res.send(d)
});
} catch(e) {
console.log(e)
}
})
return router;
}
module.exports = reg