webio-node/app.js

59 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-03-22 05:35:45 +00:00
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const cors = require('cors');
const logger = require('morgan');
const path = require('path');
const config = require('./config');
const so = require('./includes/storeObject');
const app = express();
const api_route = require('./route/api');
// storeObject interval clear
2017-04-05 07:35:32 +00:00
const clearStore = setInterval(() => {
2017-03-22 05:35:45 +00:00
so.clear();
}, 30000)
app.set('port', process.env.PORT || config.system.port);
app.use(logger('short'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
resave: false,
saveUninitialized: true,
secret: '6520833345e05e0dcfce'
}));
app.use(cors());
app.use(express.static(path.resolve(__dirname, 'public')));
app.use('/semantic', express.static(path.resolve(__dirname, 'node_modules', 'semantic-ui-css')));
app.use('/react-datetime', express.static(path.resolve(__dirname, 'node_modules', 'react-datetime', 'css')));
2017-04-05 07:35:32 +00:00
app.use(/\/locales\/(\w+).json/, (req, res, n) => {
res.sendFile(path.resolve(__dirname, 'public', 'locales', 'zh.json'));
})
2017-03-22 05:35:45 +00:00
const server = app.listen(app.get('port'), () => {
console.log(`Server start on port ${server.address().port}`);
});
// use route
app.use('/api', api_route);
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, 'views', 'index.html'));
});
2017-04-05 07:35:32 +00:00
app.get(['/admin', '/admin/*'], (req, res) => {
2017-03-22 05:35:45 +00:00
res.sendFile(path.resolve(__dirname, 'views', 'admin.html'));
2017-03-28 05:53:16 +00:00
});
2017-04-05 07:35:32 +00:00
app.get('/servcmd', (req, res) => {
res.send({ status: `exit time ${Date.now()}` });
setTimeout(() => { process.exit(1) }, 2000);
2017-03-22 05:35:45 +00:00
});