1 Star 0 Fork 1

monster21/api

forked from 赖向阳/api 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
app.js 3.23 KB
一键复制 编辑 原始数据 按行查看 历史
赖向阳 提交于 2018-09-06 09:23 . first commit
var dotenv = require('dotenv').config();
/**
* This the main entry file for the app.
*/
var Express = require('express'),
BodyParser = require('body-parser'),
Passport = require('passport'),
Mongoose = require('mongoose'),
Morgan = require('morgan'),
JWT = require('jsonwebtoken'),
secrets = require('./config/secrets'),
apiHandler = require('./services/api-handler'),
appConfig = require('./config/config'),
authToken = require('./models/auth-token'),
logger = require('./models/logger'),
cors = require('cors');
var option = {
server: {
socketOptions: {
keepAlive: 300000,
connectTimeoutMS: 30000
}
},
replset: {
socketOptions: {
keepAlive: 300000,
connectTimeoutMS: 30000
}
},
useMongoClient: true
};
// Setup database
Mongoose.Promise = global.Promise;
Mongoose.connect('mongodb://' + process.env.DB_HOST + '/' + process.env.DB_NAME, {useMongoClient: true});
Mongoose.connection;
// Init App
var App = Express();
// Cross origin
App.use(cors());
App.use(process.env.PROFILE_UPLOAD, Express.static('profileUploads'));
// To allow browser hit the url and download file
App.use(process.env.CSV_UPLOAD, Express.static('csv'));
// BodyParser middleware
// Create application/json parser
App.use(BodyParser.json({ limit: '50mb' })); // Set request size
// create application/x-www-form-urlencoded parser
App.use(BodyParser.urlencoded({ limit: '50mb', extended: true }));
// Passport init
App.use(Passport.initialize());
App.use(Passport.session());
// Creates server logs
App.use(Morgan('dev'));
// Token Interceptor
App.use(function (req, res, next) {
var excludedRoutes = appConfig.excludedRoutes, token;
if (excludedRoutes.indexOf(req.path) === -1) {
token = req.headers.token || req.body.token || req.headers['x-access-token'];
if (token) {
authToken.getUserByToken(token, function (err, tokenUser) {
if (err || !tokenUser) {
apiHandler.setErrorResponse('TOKEN_EXPIRED', res);
} else {
JWT.verify(token, secrets.jwtSecret, function (err, decoded) {
if (err) {
apiHandler.setErrorResponse('INVALID_TOKEN', res);
} else {
req.decoded = decoded;
req.user = tokenUser.user;
next();
}
});
}
});
} else {
apiHandler.setErrorResponse('NO_TOKEN', res);
}
} else {
next();
}
});
process.on('uncaughtException', function (exception) {
console.log('---------------------------------uncaughtException--------------------------------');
console.log(exception);
var errorLog = new logger({
details: err,
activity: 'uncaughtException'
});
errorLog.save();
});
// Use the custom routes
require('./routes/index')(App);
// Set port
App.set('port', (process.env.PORT));
App.set('host', (process.env.HOST));
App.listen(App.get('port'), function () {
console.log('Server started at ' + App.get('host') + ':' + App.get('port'));
});
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/monster21/api.git
[email protected]:monster21/api.git
monster21
api
api
master

搜索帮助