Skip to content

Instantly share code, notes, and snippets.

@edoves
Last active September 15, 2019 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edoves/de60d093a4dde3b01e58a965285fdc63 to your computer and use it in GitHub Desktop.
Save edoves/de60d093a4dde3b01e58a965285fdc63 to your computer and use it in GitHub Desktop.
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