-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
63 lines (50 loc) · 1.64 KB
/
app.js
File metadata and controls
63 lines (50 loc) · 1.64 KB
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
//third party module imports
const mongoose = require('mongoose');
require('dotenv').config()
const express = require('express');
const bodyParser = require('body-parser');
const hbs = require('express-handlebars')
//personal module imports
var User = require('./models/users')
// console.log(process.env.NODE_ENV)
var environment = process.env.NODE_ENV || 'development'
const stage = require('./config')[environment];
//routers
var userRouter = require('./routes/userRoutes')
var translateRouter = require('./routes/translateRoutes')
// create express server
var app = express();
//change mongoose promise library to global promise library
mongoose.Promise = global.Promise;
//create a mongoose connection
mongoose.connect(stage.db,{useNewUrlParser : true,useUnifiedTopology: true})
.catch(err => console.log("Error connecting to db :" + err))
//use body parser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.set('view engine','hbs')
app.engine('hbs',hbs({
extname : 'hbs',
layoutsDir : __dirname + '/views/layouts',
defaultLayout : 'main'
}))
app.use(express.static('public'));
var handleError = function(err,req,res,next){
if(typeof err == 'string'){
res.send("Oops,something went wrong")
}
res.status(500).send("something is wrong from our side,try login again")
}
//error handler middleware
app.use(handleError)
//user routes handler
app.use('/user',userRouter)
//translate router
app.use('/translate',translateRouter)
app.get('/',(req,res) => {
res.render('index',{})
})
app.listen(stage.port,()=>{
console.log(`node listening in port ${stage.port}`)
})
module.exports.app = app;