Created
February 26, 2017 10:27
-
-
Save slavic18/bcdc5ef35094eff6667003610498a6b7 to your computer and use it in GitHub Desktop.
Nodejs/Express example of server config.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = require('config'), | |
express = require('express'), | |
app = express(), | |
server = require('http').Server(app), | |
io = require('socket.io')(server, {'transports': ['websocket', 'polling']}), | |
bodyParser = require('body-parser'), | |
morgan = require('morgan'), | |
appConfig = require('./app/configs/config'), | |
jwt = require('jsonwebtoken'), | |
socketioJwt = require('socketio-jwt'), | |
options = { | |
server: {socketOptions: {keepAlive: 1, connectTimeoutMS: 30000}}, | |
replset: {socketOptions: {keepAlive: 1, connectTimeoutMS: 30000}} | |
}, | |
striptags = require('striptags'), | |
port = process.env.PORT || 8080, | |
mongoose = require('mongoose'); | |
mongoose.connect(config.DBHost, options); // connect to database | |
// configure app | |
app.use(bodyParser.urlencoded({extended: true})); | |
app.use(bodyParser.json()); | |
app.use(function (req, res, next) { | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
next(); | |
}); | |
//don't show the log when it is test | |
if (config.util.getEnv('NODE_ENV') !== 'test') { | |
//use morgan to log at command line | |
app.use(morgan('combined')); //'combined' outputs the Apache style LOGs | |
} | |
// controllers | |
var EventsController = require('./app/controllers/events'); | |
var SocketsController = require('./app/controllers/sockets'); | |
io.set('log level', 2); | |
// set authorization for socket.io | |
io.use(socketioJwt.authorize({ | |
secret: appConfig.secretToken, | |
handshake: true | |
})); | |
// init socket | |
SocketsController.init(io); | |
// ROUTES FOR API | |
var router = express.Router(); | |
// middleware: token dependent routes | |
router.use(function (req, res, next) { | |
// check header or url parameters or post parameters for token | |
var token = req.body.token || req.query.token || req.headers['x-access-token']; | |
if (token) { | |
// verifies secret and checks exp | |
jwt.verify(token, appConfig.secretToken, function (err, decoded) { | |
if (err) { | |
return res.json({success: false, message: 'Failed to authenticate token.'}); | |
} else { | |
if (!decoded._doc) { | |
return res.status(403).send({ | |
success: false, | |
message: 'Token is not valid' | |
}); | |
} | |
// if everything is good, save to request for use in other routes | |
req.decoded = decoded; | |
next(); | |
} | |
}); | |
} else { | |
// if there is no token return an error | |
return res.status(403).send({ | |
success: false, | |
message: 'No token provided.' | |
}); | |
} | |
}); | |
// events | |
router.route('/events') | |
.post(function (req, res) { | |
EventsController.post(req, res); | |
}) | |
.get(function (req, res) { | |
EventsController.get(req, res); | |
}) | |
.delete(function (req, res) { | |
EventsController.delete(req, res); | |
}); | |
router.route('/events/:event_id') | |
.get(function (req, res) { | |
EventsController.getById(req, res); | |
}) | |
.put(function (req, res) { | |
EventsController.update(req, res); | |
}) | |
.delete(function (req, res) { | |
EventsController.delete(req, res); | |
}); | |
// REGISTER OUR ROUTES ------------------------------- | |
app.use('/api', router); | |
// START THE SERVER | |
// ============================================================================= | |
server.listen(port); | |
module.exports = app; // for testing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment