This repository has been archived by the owner on Nov 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
64 lines (47 loc) · 2.15 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
//here we will handle the rquests
const express=require('express');// we are importing the package 'express'
const bodyParser=require('body-parser');
require('dotenv/config');
const app=express();
const morgan=require('morgan');
//Middlewares--will execute when we run a route
app.use(bodyParser.urlencoded({extended:true}));//true allows us to extend complex bodies
app.use(morgan('dev'));
app.use((req,res,next)=>{
res.header('Allow-Control-Allow-Origin','*')//here we want to prevent errors concerning headers and we will allow it to be used by any client hence the use of '*'
res.header('Allow-Control-Allow-Headers','Origin,X-Requested-With,Content-Type,Accept,Authorization');//all these headers will be appended to an incoming request
/**
* A browser will always send an options request first when we send a PUT or post request
*
*/
if(req.method ==='OPTIONS'){
//no we tell the browser what it may send
res.header('Access-Control-Allow-Methods','PUT,POST,PATCH,DELETE,GET');//all the requests our api is going to be executing
return res.status(200).json({});
}
next();
});
//now you have THE ABILITY TO CREATE ROUTES
//import routes
const disasterRoute=require('./api/routes/disasterRoute');
/*app.use((req,res,next)=>{//it setsup a middleware and an incoming request will come thru this function
response.status(200).json({
message:"Data sent"
});
});*/
//when the link is entered, it will be directed to the disasterRoute file
//instead of using app.use('/disaster',disasterRoute); use the one below
app.use('/api/v1/',disasterRoute);
//now we want to handle all requests and errors coming from requests
app.use((req,res,next)=>{
const error=new Error('Not Found');
error.status=404;
next(error);//will forward the error request incase of an error
});
//from there we create a middleware to handle all sorts of errors
app.use((error,req,res,next)=>{
res.status(error.status || 500); //will be used to handle all other forms of errors including the one we created
res.send(error.message);
});
//but first we set a listener to connect to the server
module.exports=app;