-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
64 lines (53 loc) · 2.37 KB
/
main.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
const express= require('express');
const bodyParser = require('body-parser')
const app = express();
app.use(express.static('public')) // it avoids the use of public in any path.
const { check, validationResult } = require('express-validator'); // for valid error and message
const {matchedData, sanitizeBody} = require('express-validator');
var ourApi=require('./api/add-category');
var ourRoutes=require('./routes/index');
app.use('/api', ourApi);
/* for the template engine */
//app.set('view engine', 'twig') // for the templating engine
app.set('view engine', 'ejs') // for the templating engine
app.set('views', './public/views') // for the templating engine
/* for the body parser */
app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded
app.use(bodyParser.json()) // parse application/json
/* for the express validator */
app.use('/routes', ourRoutes);
app.get('/', function (req, res) {
res.render('index1', { title: 'Heymm', message: 'Hello therenbnbnb!' })
})
app.post('/',[
// username must be an email
check('username','username should be email id' ).isEmail(),
// password must be at least 5 chars long
check('password', 'must be 5 in length').isLength({ min: 5 }),
check('cpassword', 'must be 5 in length').isLength({ min: 5 })
] ,function (req, res) {
// Finds the validation errors in this request and wraps them in an object with handy functions
const errors = validationResult(req);
console.log(errors.mapped());
console.log(req.body);
if(!errors.isEmpty())
{
const user= matchedData(req);
res.render('index', { userdetails:'Here we have user details',error:errors.mapped(),user:user })
}
else
{
//const allData = matchedData(req);
//console.log("Here is all data"+allData);
const user= matchedData(req);
console.log(user);
//res.render('login', { userdetails:'Here we have user details',username: req.body.username, password: req.body.password})
res.render('login', { userdetails:'Here we have user details',user:user})
}
})
app.get('/about/:a-:b', function (req, res) {
res.render('about', { title: 'Heymm', message: 'Hello Here is about page section!',
'add':parseInt(req.params.a)+parseInt(req.params.b), mul:parseInt(req.params.a)*parseInt(req.params.b)})
})
// must use this to listen on some port
app.listen(3000,()=>console.log("server running on port 3000"));