我有一个在端口3000上运行的服务器。现在有一个问题,当我在那时使用我的MongoDB集合进行繁重的计算,如果请求来自前端,那么前端用户必须等到后端进程是执行。我想到了两个服务器,一个是处理前端请求,另一个是使用MongoDB进行大量计算。 我为单个服务器实现的是:
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var exphbs = require('express-handlebars');
var expressValidator = require('express-validator');
var flash = require('connect-flash');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var mongo = require('mongodb');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://127.0.0.1/my_db_name', { useMongoClient: true });
var db = mongoose.connection;
//Initialize App
var app = express();
app.disable('x-powered-by');
//View Engine
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({defaultLayout: 'layout'}));
app.set('view engine', 'handlebars');
//BodyParser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true }));
app.use(cookieParser());
//Set Static Folder
app.use(express.static(path.join(__dirname, 'public')));
//Express Session
app.use(session({
secret: 'shhhhh',
saveUninitialized: true,
resave: true,
cookie:{maxAge: 86400000},// For 24 hour
store: new MongoStore({ mongooseConnection: db })
}));
//Set Port
app.set('port', (process.env.PORT || 3000));
app.listen(app.get('port'), function(){
console.log('Server Started on Port '+app.get('port'));
});
那么在不同的端口上运行多个实例需要添加哪些内容?