-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
80 lines (64 loc) · 2.59 KB
/
Copy pathapp.js
File metadata and controls
80 lines (64 loc) · 2.59 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var express=require('express');//install express js
var bodyParser = require('body-parser');//body parser for post form data
var mongoose = require('mongoose');//mongoose db require
var Schema = mongoose.Schema; //mongoose schema
mongoose.Promise = require('bluebird');
var app=express();//expressjs initialization
app.set('view engine','ejs');//install template engine
app.use("/styles",express.static(__dirname + "/styles"));//include static files
//simple routing in express
app.get('/',function(req,res){ //home page
var title='Welcome to nodejs demo';
res.render(__dirname+'/views/index',{data:'welcome to home page',title:title});
});
app.get('/about',function(req,res){ //about page
res.render(__dirname+'/views/about',{data:'This is about page',title:'about'});
});
app.get('/profile',function(req,res){// profile
res.render(__dirname+'/views/profile',{data:'This is profile page',title:'profile'});
});
app.get('/profile/:name',function(req,res){ //profile query string page
//res.send('this is profile page'+req.params.name);
res.sendFile(__dirname+'/profile');
});
app.get('/link1',function(req,res){ // link page
res.render(__dirname+'/views/link1',{data:'This is link page',title:'link1'});
});
app.get('/link2',function(req,res){
res.render(__dirname+'/views/link2',{data:'This is link2 page',title:'link2'});
});
app.get('/link3',function(req,res){
res.render(__dirname+'/views/link3',{data:'This is link3 page',title:'link3'});
});
app.get('/contact',function(req,res){ //contact page
res.render(__dirname+'/views/contact',{data:'This is contact page',title:'contact'});
});
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
// POST /login gets urlencoded bodies
app.post('/contact-success', urlencodedParser, function (req, res) {
if (!req.body) {//if not successfull
return res.sendStatus(400);
} else {
//post request form data here
var data=req.body;
//schema for table TestContact
var schemaName = new Schema({
fname:String,
email:String,
phone:Number,
url:String,
message:String
});
var TestContact = mongoose.model('TestContact', schemaName);//create table with schema TestContact
mongoose.connect('mongodb://test:test@ds133932.mlab.com:33932/mongotest');//connect to mongodb of https://mlab.com/
TestContact(data).save(function(err){
if(err) throw err;
console.log('item saved');
});
//now reder the contact success page.
res.render(__dirname+'/views/contact-success',{contactdata:data,title:'contact us success!'});
}
});
//Server listen to localhost in this port number
app.listen(8080);