This repository has been archived by the owner on Sep 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·68 lines (56 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
const express = require('express'); // da cai
const mongoose = require('mongoose'); // da cai
const session = require('express-session'); // da cai
const MongoStore = require('connect-mongo')(session);
const app = express();
// Mongo connection
// TODO: Explaination HERE about name replacement (localhost -> mongo)
// more info: https://itnext.io/dockerize-a-node-js-app-connected-to-mongodb-64fdeca94797
mongoose.connect('mongodb://mongo:27017/weather1');
let db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error'));
// Dùng Sessions để theo dõi logins
app.use(
session({
secret: 'Its me',
resave: true,
saveUninitialized: false,
store: new MongoStore({
mongooseConnection: db
})
})
);
// Làm user ID thành 1 templates
app.use(function(request, response, next) {
response.locals.currentUser = request.session.userId;
next();
});
// Parse data
app.use(express.urlencoded({ extended: false }));
// serve static files from /public
app.use(express.static(__dirname + '/public'));
// view engine setup
app.set('view engine', 'pug');
app.set('views', __dirname + '/views');
// Route
const mainRoutes = require('./routes');
const weatherRoutes = require('./routes/weather.js');
app.use(mainRoutes);
app.use('/weather', weatherRoutes);
// bắt sự kiện lỗi 404 và chuyển cho error handler
app.use(function(request, response, next) {
var err = new Error('File Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, request, response, next) {
response.status(err.status || 500);
response.render('error', {
message: err.message,
error: {}
});
});
app.listen(3000, () => {
console.log('Sever is running now at port 3000');
});