-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
60 lines (46 loc) · 1.21 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
var express = require('express');
var morgan = require('morgan');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var ejs = require('ejs');
var ejsMate = require('ejs-mate');
var User = require('./models/user');
var app = express();
var PORT = process.env.PORT || 3000;
mongoose.connect('mongodb://root:[email protected]:11943/ecommercenode',function(err){
if(err) {
console.log(err);
}else{
console.log('Connected to the database');
}
})
//Middleware
app.use(express.static(__dirname + '/public'));
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.engine('ejs',ejsMate);
app.set('view engine', 'ejs');
app.get('/',function(req,res){
res.render('main/home');
});
app.get('/about',function(req,res){
res.render('main/about');
})
app.post('/users',function(req,res,next){
var user = new User();
user.profile.name = req.body.name;
user.password = req.body.password;
user.email = req.body.email;
user.save(function(err){
if(err){
return next(err);
}else{
res.json('Succesfully created a new user');
}
});
});
app.listen(PORT, function(err){
if(err) throw err;
console.log("Server is Running on port 3000")
});