Skip to content

Instantly share code, notes, and snippets.

@edoves edoves/app.js
Last active Sep 15, 2019

Embed
What would you like to do?
Sample server Express
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;
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;
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;
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
You can’t perform that action at this time.