-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
72 lines (62 loc) · 2.39 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
const express = require ('express')
const morgan = require ('morgan')
const bodyParser= require ('body-parser')
const cors = require ('cors')
const mongoose = require ('mongoose')
const session = require('express-session');
const multer = require('multer');
const path = require('path');
const favicon = require('serve-favicon');
// const logger = require('morgan');44444
const routes = require('./routes')
/*we create these constants to import the above modules using keyword "require". */
const app=express()
//app.use(session({secret: 'ssshhhhh',saveUninitialized: true,resave: true}));
app.use(morgan('tiny'))
app.use(cors())
app.use(bodyParser({extended:true}))
app.use(async (req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', '*');
res.setHeader('Access-Control-Allow-Credential', true);
res.setHeader('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, HEAD, OPTIONS');
next();
});
app.use(express.static(__dirname + '/static'));
/*Above code is used to use a particular module imported above. */
// const storage = multer.diskStorage({ //multers disk storage settings
// destination: function (req, file, cb) {
// cb(null, './uploads/')
// },
// filename: function (req, file, cb) {
// var datetimestamp = Date.now();
// cb(null, file.fieldname + '-' + datetimestamp + '.' + file.originalname.split('.')[file.originalname.split('.').length -1])
// }
// });
// const upload = multer({ //multer settings
// storage: storage
// }).single('file');
// app.post('/upload', function(req, res) {
// upload(req,res,function(err){
// if(err){
// res.json({error_code:1,err_desc:err});
// return;
// }
// res.json({error_code:0,err_desc:null});
// })
// });
// var sess;
// app.get('/',(req,res) => {
// sess = req.session;
// if(sess.username) {
// return res.redirect('/admin');
// }
// res.sendFile('');
// });
mongoose.connect('mongodb://127.0.0.1:27017/todo',{useNewUrlParser : true})
.then(()=>{console.log('database sucessfully connected')})
.catch((err)=>{console.log(err)})
app.get('/',(req,res)=>{res.send({"home":"Welcome..."})})
app.get('/getmsg',(req,res)=>{res.send({"message":"Hello World!"})})
app.listen(8000,'localhost',()=>{console.log('server started!')})
routes(app)