-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (57 loc) · 1.97 KB
/
index.js
File metadata and controls
75 lines (57 loc) · 1.97 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
// import statements
const bodyParser = require('body-parser')
const express = require('express')
const app = express()
// import file from utilities
const HttpError = require('./utilities/http-error');
// import file from routes
const userroutes = require('./routes/user-routes');
const adminroutes = require('./routes/admin-routes');
// configuration statements
const port = 3002;
app.use(bodyParser.json);
// routing
app.use("/api/v1/user",userroutes);
app.use("/api/v2/admin",adminroutes);
app.use((req,res,next) =>{
//creatign custom error
const error = new HttpError("page not found",404);
throw error;
});
app.use((error,req,res,next) => {
res.status(error.code);
res.json({"message":"error.message" || "An unknown error occured",code:error.code})
})
// // main methods
// app.get('/about', (req, res) => {
// res.send(JSON.stringify({page:"about",message:"about page"}))
// })
// app.post('/login',(req,res) =>{
// res.send(JSON.stringify({page:"login",message:"this is login page"}))
// })
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
// Import Statements
// const express = require('express');
// const app = express();
// const bodyParser = require('body-parser');
// // const mongoose = require('mongoose');
// const userRoutes = require('./routes/user-routes');
// const adminRoutes = require('./routes/admin-routes');
// const HttpError = require('./utils/http-error');
// // Configuration statements
// const port = 3001;
// app.use(body-parser.json());
// // Routing
// app.use('/api/v1/user', userRoutes);
// app.use('/api/v1/admin', adminRoutes);
// // Error Handling
// app.use((req,res,next) => {
// const error = new HttpError('Page not found',404);
// throw error;
// });
// app.use((error, req, res, next) => {
// res.status(error.code);
// res.json({message: error.message || 'Unknown error occured' , code: error.code });
// });