-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (56 loc) · 1.77 KB
/
server.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
const express = require('express');
const path = require('path');
const Layouts = require("express-ejs-layouts");
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const helpers = require('./helpers');
const app = express();
require('dotenv').config({path:'.env'});
mongoose.connect(process.env.dbURL);
mongoose.Promise = global.Promise;
mongoose.connection.on('error',(err) =>{
console.log(`mongoDB Error : ${err.message} \n Please start mongoDB server `);
});
//view engine set-up
app.set('view engine','ejs');
app.set('views',path.join(__dirname,'views'));
app.use(express.static(path.join(__dirname,'public')));
app.use(Layouts);
// configure the app to use bodyParser()
app.use(bodyParser.urlencoded({
extended:true
}));
app.use(bodyParser.json());
app.set('port',process.env.PORT || 3000);
let siteName = 'Accurate Weather Application';
app.use((req,res,next)=>{
res.locals.siteName = siteName;
res.locals.h = helpers;
res.locals.watherURL = process.env.watherURL;
res.locals.APPID = process.env.APPID;
next();
})
//route define
const index = require('./routes/index');
const location = require('./routes/location');
app.use('/',index);
app.use('/location',location);
//catch 404 and forward to error handle
app.use((req,res,next)=>{
const error = new Error('Not Found');
error.status = 404;
next(error);
});
//Error Handler
app.use((error,req,res,next) =>{
console.log(error);
res.locals.message = error.message;
res.locals.status = error.status;
res.locals.error = app.get('env') === 'development' ? error : {};
// render the error page
res.status(error.status || 500);
res.render('errors');
});
app.listen(app.get('port'),function(){
console.log('Listening on port :'+app.get('port'));
})