-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·71 lines (57 loc) · 1.72 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env node
const express = require('express');
const handlebars = require('express-handlebars');
const bodyParser = require('body-parser');
const session = require('express-session');
// import environment variables
const dotenv = require('dotenv');
dotenv.config();
// app server
const app = express();
// define using session cookies
app.use(session({
secret: '5119497770', // some random string
resave: false,
saveUninitialized: false,
}));
// enable for forms reading
app.use(bodyParser.urlencoded({extended: true}));
// use folder 'public' as static folder for assets
app.use(express.static('./public'));
// and the crisis database (geojson files for countries and regions)
app.use(express.static('./crisis_database'));
// and all node modules
app.use(express.static('./node_modules'));
// define handlebars as the view engine
app.engine('.hbs', handlebars.engine({extname: '.hbs'}));
app.set('view engine', '.hbs');
app.set('views', './views');
// define where the routes are
const routes = require('./routes');
app.use('/', routes);
// define 404 not found
app.use(function (req, res, _) {
res.status(404);
// respond with html page
if (req.accepts('html')) {
const viewData = {
title: '404 Not Found',
id: '404',
layout: 'info'
};
res.render('404', viewData);
} else
// respond with json
if (req.accepts('json')) {
res.json({error: 'Not found'});
} else
// default to plain-text. send()
{
res.type('txt').send('Not found');
}
});
// listen on port 3000 (in env variables)
app.listen(process.env.PORT, () => {
console.log(`CrisisMap listening on ${process.env.PORT}`);
});
module.exports = app;