当前位置:网站首页>Nodejs: set up the express service, set up the session and realize the exit operation
Nodejs: set up the express service, set up the session and realize the exit operation
2022-07-28 10:55:00 【shunzi2016】
const plug_i18next = require('i18next');
const plug_i18nextMiddleware = require('i18next-express-middleware');
const plug_Backend = require('i18next-node-fs-backend');
var plug_express = require('express');
var plug_path = require('path');
var plug_methodOverride = require('method-override');
var plug_session = require('express-session');
var plug_cookieParser = require('cookie-parser');
var plug_multiparty = require('multiparty');
var plug_jade = require('jade');
var plug_favicon = require('serve-favicon');
var child_process = require('child_process');
var plug_errorHandler = require('errorhandler');
var plug_request = require('request');
var plug_httpproxy = require('http-proxy');
var plug_proxy = require('http-proxy-middleware');
var fs = require('fs');
var http = require('http');
var https = require('https');
var app = plug_express();
var jwt = require('jsonwebtoken'); // Use jwt Signature
var PORT = 80;
var SSLPORT = 8888;
var TimeOut = 3600;
app.set('port', SSLPORT);
app.use(plug_session({
/* I could just write . One String String of type , As a server-side generation session The signature of the */
secret: 'QuotPXt',
name: 'sQt_KYBGRExdetyhLMN0387650XXD',
/* Keep it locally cookie A name for Default connect.sid You can leave it blank */
/* Force save session Even if it doesn't change ,. The default is true. It is suggested to set it to false.*/
resave: false,
/* Force uninitialized session Storage . The default value is true It is suggested to set it to true*/
saveUninitialized: true,
cookie: {
maxAge: 1000 * 30 * 60 /* Expiration time */
},
/*secure https Only in this way can we access cookie*/
// Set the expiration time, such as 30 minute , Just visit the page ,30 Minutes will expire if there is no operation
// Force settings on every request cookie, This will reset cookie Expiration time ( Default :false)
rolling: true
}));
app.use(plug_cookieParser());
var originalFilename = "";
app.use(plug_express.static(plug_path.join(__dirname, 'static')));
app.use(plug_i18nextMiddleware.handle(plug_i18next));
// Merge g_dictRootFromHeader To root in
for (var key in js_headjsonhandle.g_dictRootFromHeader) {
root[key] = js_headjsonhandle.g_dictRootFromHeader[key];
}
for (var key in root) {
if (key) {
renderCommon(key)
}
}
// Turn on HTTPS service
function func_startHTTPS() {
//TODO
var privateKey = fs.readFileSync('./static/cert/server.key', 'utf8');
var certificate = fs.readFileSync('./static/cert/server.crt', 'utf8');
var credentials = { key: privateKey, cert: certificate };
var httpsServer = https.createServer(credentials, app);
httpsServer.listen(app.get('port'), '0.0.0.0', function() {
console.log('HTTPS Server is running on: https://localhost:%s', app.get('port'));
});
httpsServer.on('error', (e) => {
console.error(e);
});
function logout(req, res, next) {
new Promise((resole, rej) => {
var res_data = {};
req.session.destroy(function(err) {
console.log(err);
})
res_data.code = 0;
res_data.msg = " Quit successfully ";
resole(res_data);
}).then((res_data) => {
if (res_data) {
return res.json(res_data);
}
});
}
//404
app.all('*', function(req, res, next) {
var key = req.url;
if (req.path == '/CE/logout') {
logout(req, res, next);
} else {
res.redirect('/404');
}
next()
});
}
// Login website
app.get('/', function(req, res, next) {
let host = req.headers.host;
host = host.replace(/\:\d+$/, '');
if (!req.cookies.token || req.cookies.token == '0' || req.cookies.token == 'null') {
if (req.protocol === 'https') {
return res.redirect(`/login`);
} else {
if (SSLPORT == 443) {
return res.redirect(307, `https://${host}/login`);
} else {
return res.redirect(307, `https://${host}:${SSLPORT}/login`);
}
}
} else {
if (req.protocol === 'https') {
return res.redirect(`/sa_sec/main_page`);
} else {
if (SSLPORT == 443) {
return res.redirect(307, `https://${host}/sa_sec/main_page`);
} else {
return res.redirect(307, `https://${host}:${SSLPORT}/sa_sec/main_page`);
}
}
}
next()
});
function func_startHTTP() {
var httpServer = http.createServer(app);
httpServer.listen(PORT, '0.0.0.0', function() {
console.log('HTTP Server is running on: http://localhost:%s', PORT);
});
}
function startServer() {
func_startHTTP();
func_startHTTPS();
}
startServer();边栏推荐
- ICML 2022 | graph represents the structure aware transformer model of learning
- GKLinearCongruentialRandomSource
- GKVoronoiNoiseSource
- 21. Merge two ordered linked lists
- The 10th Landbridge cup embedded electronic provincial competition
- STM32中的APB2和APB1
- Select without the order by clause, the order of the returned results is not reliable
- Invalid ROM Table原因及解决办法
- Judge whether the nixie tube is a common anode or a common cathode
- GKObstacle
猜你喜欢
随机推荐
两年CRUD,二本毕业,备战两个月面试阿里,侥幸拿下offer定级P6
判断数码管是共阳极还是共阴极
GKNoiseMap
Install GMP
02.1.2. logic type bool
剑指 Offer 30. 包含min函数的栈
Tree Shaking和DCE
粒子群实现最优解的求解
GKConstantNoiseSource
GKVoronoiNoiseSource
Preliminary understanding of float
GKCheckerboardNoiseSource
3. MapReduce explanation and source code analysis
GKARC4RandomSource
The 10th Landbridge cup embedded electronic provincial competition
11_ue4进阶_男性角色换成女性角色,并修改动画
GKCheckerboardNoiseSource
分体式测斜探头安装要点及注意事项
Pyqt5 rapid development and practice 4.11 drag and clipboard
10_ue4进阶_添加倒地和施法动作









