nodejs-boilerplate/app.js

63 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2020-03-29 20:41:16 +00:00
/*## Dependencies ##*/
const express = require('express'),
app = express(),
bodyParser = require('body-parser'),
path = require('path'),
session = require('express-session'),
flash = require('connect-flash'),
methodOverride= require('method-override'),
morgan = require('morgan');
/*## link your route files here ##*/
2020-03-29 20:41:16 +00:00
const indexRoute = require('./routes/index'),
apiRoute = require('./routes/api');
/*## Now your app configuration ##*/
2020-03-29 20:41:16 +00:00
if (app.get('env') === 'development'){
app.use(morgan('dev')); // log every request to the console for development
}
app.use(express.static(path.join(__dirname, 'public'))); // folder for public serving (images, files, scripts)
2020-03-29 20:41:16 +00:00
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
var sess = {
secret: "ChooseSessionString", //change this to your custom string
2020-03-29 20:41:16 +00:00
resave: true,
saveUninitialized: true,
cookie: { maxAge: 1000 * 60 * 60 * 24 * 7 } //how long should cookies been saved
};
if (app.get('env') === 'production') { //secure sessions for production
2020-03-29 20:41:16 +00:00
app.set('trust proxy', 1) // trust first proxy
sess.cookie.secure = true // serve secure cookies
}
app.use(session(sess)); //init sessions with above configuration
2020-03-29 20:41:16 +00:00
app.set('views', __dirname + '/views'); //folder with view pages
app.set("view engine", "pug"); //set view engine to pug
app.set('view options', { //set custom view options
layout: false //set layout to false to use more than one and use pug
2020-03-29 20:41:16 +00:00
});
app.use(flash()); //to use connect-flash in your environment
app.use(methodOverride("_method")); //override with POST having ?_method=CUSTOM
/*## repeated actions for each website call ##*/
2020-03-29 20:41:16 +00:00
app.use(function(req, res, next){
res.locals.success = req.flash('success'); //save your flash-success-messages locally
res.locals.error = req.flash('error'); //save your flash-error-messages locally
res.locals.currentUser = req.user; //save your current user locally for quick l
2020-03-29 20:41:16 +00:00
next();
});
/*## Routes ##*/
// note that NodeJS will choose the first option available that matches the pattern
2020-03-29 20:41:16 +00:00
app.use("/api", apiRoute);
app.use("/", indexRoute);
app.get('*', (req,res)=>{ res.redirect("/"); }); //fallback: redirect all website calls with no matching route to homesite (or 404 page)
2020-03-29 20:41:16 +00:00
/*## init server ##*/
app.listen(process.env.PORT, process.env.IP, function(){ //start your server
2020-03-29 20:41:16 +00:00
console.log("Server is listening at " + process.env.IP + ":" + process.env.PORT + " with Environment: " + process.env.env);
});