Created
December 6, 2020 22:07
-
-
Save soonsam123/31bbd5d6ab03fefe4dd9a33ba21004cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const dojosRoutes = require("./api/routes/dojos"); | |
const app = express(); | |
app.listen(8000); | |
/** | |
* Adding headers to our requests. | |
*/ | |
app.use((req, res, next) => { | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header( | |
"Access-Control-Allow-Headers", | |
"Origin, X-Requested-With, Content-Type, Accept, Authorization" | |
); | |
if (req.method === "OPTIONS") { | |
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); | |
return res.status(200).json({}); | |
} | |
next(); | |
}); | |
app.use("/dojos", dojosRoutes); | |
app.use((req, res, next) => { | |
const error = Error("Not found"); | |
res.statusCode = 404; | |
res.send({ error: error.message }); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment