Last active
September 15, 2019 02:54
-
-
Save edoves/de60d093a4dde3b01e58a965285fdc63 to your computer and use it in GitHub Desktop.
Sample server Express
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 app = express(); | |
// logger | |
const morgan = require("morgan"); | |
const productRoutes = require("./api/routes/products"); | |
const orderRoutes = require("./api/routes/orders"); | |
// using the morgan the resul will beo nthe terminal | |
app.use(morgan("dev")); | |
// avoid Cross-Origin Request Blocked | |
app.use((req, res, next) => { | |
res.header("Acces-Control-Allow-Origin", "*"); | |
res.header( | |
"Acces-Control-Allow-Headers", | |
"Origin", | |
"X-Requested-With", | |
"Content-Type", | |
"Accept", | |
"Authorization" | |
); | |
/** | |
* ! Browsers is alwatys send an OPTION request first | |
* ! when you send a POST request or PUT request | |
*/ | |
if ((req.method = "OPTIONS")) { | |
/** | |
* ! Addional header to tell the browser what it may send | |
*/ | |
res.header("Acces-Control-Allow-Methods", "PUT, POST, PATCH, DELETE, GET"); | |
return res.status(200).json({}); | |
} | |
next(); | |
}); | |
// parsers | |
app.use(express.urlencoded({ extended: false })); | |
app.use(express.json()); | |
app.use("/products", productRoutes); | |
app.use("/orders", orderRoutes); | |
/** | |
* ! handling error for the whole application route or url request | |
* ! it will fires if it did not find the fitting route | |
*/ | |
app.use((req, res, next) => { | |
const error = new Error("Not found"); | |
error.status = 404; | |
next(error); | |
}); | |
app.use((error, req, res, next) => { | |
res.status(error.status || 500); | |
res.json({ | |
error: { | |
message: error.message | |
} | |
}); | |
}); | |
module.exports = app; |
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 router = express.Router(); | |
router.get("/", (req, res) => { | |
res.status(200).json({ | |
message: "Orders were fetched" | |
}); | |
}); | |
router.post("/", (req, res) => { | |
res.status(201).json({ | |
message: "Order was created" | |
}); | |
}); | |
router.post("/:orderId", (req, res) => { | |
res.status(200).json({ | |
message: "Order details", | |
orerId: req.params.orderId | |
}); | |
}); | |
router.delete("/:orderId", (req, res) => { | |
res.status(200).json({ | |
message: "Order deleted", | |
orerId: req.params.orderId | |
}); | |
}); | |
module.exports = router; |
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 router = express.Router(); | |
router.get("/", (req, res) => { | |
res.status(200).json({ | |
message: "Handling GET request to /products" | |
}); | |
}); | |
router.post("/", (req, res) => { | |
res.status(200).json({ | |
message: "Handling POST request to /products" | |
}); | |
}); | |
router.get("/:productid", (req, res) => { | |
const id = req.params.productid; | |
if (id === "special") { | |
res.status(200).json({ | |
message: "You discovered the special ID", | |
id: id | |
}); | |
} else { | |
res.status(200).json({ | |
message: "You passed an ID" | |
}); | |
} | |
}); | |
router.patch("/:productid", (req, res) => { | |
res.status(200).json({ | |
message: "Updated product" | |
}); | |
}); | |
router.delete("/:productid", (req, res) => { | |
res.status(200).json({ | |
message: "Deleted product" | |
}); | |
}); | |
module.exports = router; |
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 http = require("http"); | |
const app = require("./app"); | |
const port = process.env.PORT || 3000; | |
const server = http.createServer(app); | |
server.listen(port, () => { | |
console.log(`Server running on port ${port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment