-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
101 lines (83 loc) · 3.14 KB
/
app.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
const express = require('express');
const { query, checkSchema, validationResult } = require('express-validator');
const mongoose = require('mongoose');
const request = require('request')
const app = express();
const Address = require('./models/address');
const { validationRules } = require('./objects')
const API_KEY = process.env.API_KEY;
app.set('view engine','ejs');
app.use(express.static('public'));
app.use(express.urlencoded({extended:true}));
mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true, useUnifiedTopology: true }, function(error) {
console.log((error) ? 'Connection failed: Can\'t connect to Database':'Database Connected');
});
app.get('/',function(req,res){
res.render('home');
});
// Route for fetching addreses form database and rendering on client side
app.get('/:userId/addresses',checkSchema({userId:validationRules.userId}),function(req,res){
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
const userId = req.params.userId;
Address.find({ userId: userId }, -'_id', function(err,addresses){
if (err){
return res.status(500).json({msg:'fail',err: "Internal server error"});
}
return res.render('address', { addresses: addresses, userId: userId });
});
});
// Route for rendering address form
app.get('/:userId/addresses/form',checkSchema({userId:validationRules.userId}),function(req,res){
return res.render('form',{ userId: req.params.userId, errors:[] })
});
// Route for posting data to database
app.post('/:userId/addresses', checkSchema(validationRules), function(req,res){
//console.log(req.body);
const errors = validationResult(req);
const userId = req.params.userId;
if (!errors.isEmpty()) {
return res.render('form',{ errors: errors.errors, userId:req.params.userId });
}
Address.create({ userId: userId, ...req.body})
.then(function(data){
return res.redirect(`/${userId}/addresses`);
})
.catch( err=>{
return res.send("Unexpected Error");
});
});
// Route for fetching data from external API
app.get('/getlocation',query('latLong').trim().isLatLong(),function(req,res){
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
let lat = req.query.latLong.split(',')[0];
let lng = req.query.latLong.split(',')[1];
request(`https://api.geodatasource.com/city?key=${API_KEY}&lat=${lat}&lng=${lng}`, function (error, response, body) {
if (error)
res.status(500).json({
msg:'fail',
detail:'Somthing went wrong'
});
body = JSON.parse(body);
if (response.statusCode && body.region && body.city) // Print the response status code if a response was received
res.status(200).json({
msg:'success',
state:body.region,
city:body.city
});
else {
res.status(500).json({
msg: 'fail',
error: body.error
});
}
});
});
app.listen(process.env.PORT || 3000, function(){
console.log(process.env.PORT);
});