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');
|
|
|
|
|
2020-03-31 12:57:20 +00:00
|
|
|
/*## link your route files here ##*/
|
2020-03-29 20:41:16 +00:00
|
|
|
const indexRoute = require('./routes/index'),
|
|
|
|
apiRoute = require('./routes/api');
|
|
|
|
|
2020-03-31 12:57:20 +00:00
|
|
|
/*## 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
|
|
|
|
}
|
|
|
|
|
2020-03-31 12:57:20 +00:00
|
|
|
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 = {
|
2020-03-31 12:57:20 +00:00
|
|
|
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
|
|
|
|
};
|
2020-03-31 12:57:20 +00:00
|
|
|
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
|
|
|
|
}
|
2020-03-31 12:57:20 +00:00
|
|
|
app.use(session(sess)); //init sessions with above configuration
|
2020-03-29 20:41:16 +00:00
|
|
|
|
2020-03-31 12:57:20 +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
|
|
|
});
|
|
|
|
|
2020-03-31 12:57:20 +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){
|
2020-03-31 12:57:20 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2020-03-31 12:57:20 +00:00
|
|
|
/*## 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);
|
2020-03-31 12:57:20 +00:00
|
|
|
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
|
|
|
|
2020-03-31 12:57:20 +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);
|
|
|
|
});
|