forked from TopiNiskala/lahimatkailu-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
108 lines (82 loc) · 2.85 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import express from 'express';
import bodyParser from 'body-parser';
import morgan from 'morgan';
import mongoose from 'mongoose';
import router from './router';
import passport from 'passport';
import User from './models/user';
import cookieParser from 'cookie-parser';
//import users from './controllers/users';
//import session from 'express-session'
//import LocalStrategy from 'passport-local';
var mongo = require('mongodb');
var path = require('path');
var session = require('express-session');
var LocalStrategy = require('passport-local').Strategy;
import i18n from 'i18n-2';
const app = express();
app.use(cookieParser());
app.use(session({
secret: "tHiSiSasEcRetStr",
resave: false,
saveUninitialized: false }));
app.use(passport.initialize());
app.use(passport.session());
//_______________________________________________
//kovakoodattu testin ajaksi
// users = [{"_id":111, "username":"moi", "password":"moi"}];
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
//app.use(bodyParser.urlencoded({ extended: false }));
i18n.expressBind(app, {
locales: ['en', 'fi', 'sv', 'ru'],
cookieName: 'locale',
defaultLocale: 'en',
devMode: true
});
app.use(function(req, res, next) {
req.i18n.setLocaleFromCookie();
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended:false,
limit: '15mb' }), function (error, req, res, next) {
if (error instanceof Error) {
return res.send('500', { error: error });
} else {
next();
}
});
// route middleware to ensure user is logged in
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.sendStatus(401);
};
//_________________________________________________
//This file connects our server to mongoDB and uses the router we have created
mongoose.connect('mongodb://localhost/kohteet');
// Initialize http server
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('We are connected to MongoDB!');
});
//__________________________________________________
// Logger outputting all requests in to the console
app.use(morgan('combined'));
// Use v1 as prefix for your API endpoints
app.use('/v1', router);
// Serve javascript files from js subdirectory
app.use("/js", express.static(__dirname + "/assets/js"));
// Serve images from img subdirectory
app.use("/img", express.static(__dirname + "/assets/img"));
// Serve css files from css subdirectory
app.use("/css", express.static(__dirname + "/assets/css"));
app.set('view engine', 'pug');
// Launches server on port 3000
const server = app.listen(3000, () => {
const { address, port } = server.address();
console.log(`Listening at http://${address}:${port}`);
});