Last active
May 30, 2021 05:18
-
-
Save achukka/8676445ece0059d07b61a178d03dc44e to your computer and use it in GitHub Desktop.
Route for Items in Express Server
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
import express from "express"; | |
import { router as item_router } from "./items"; | |
const app = express(); | |
const port = 3000; | |
app.use("/tdsvc/item", item_router); | |
app.listen(port, (err?) => { | |
if (err) { | |
return console.error(err); | |
} | |
return console.log(`server is listening on port: ${port}`); | |
}); |
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
import express from "express"; | |
import { router as item_router } from "./items"; | |
import { router as user_router } from "./users"; | |
const app = express(); | |
const port = 3000; | |
app.use("/tdsvc/item", item_router); | |
app.use("/tdsvc/user", user_router); | |
app.listen(port, (err?) => { | |
if (err) { | |
return console.error(err); | |
} | |
return console.log(`server is listening on port: ${port}`); | |
}); |
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
import express from "express"; | |
export const router = express.Router(); | |
// GET Method | |
router.get("/:id", (req, res) => { | |
res.send(`You are requesting an item with id: ${req.params["id"]}`); | |
}); | |
// POST Method | |
router.post("/", (req, res) => { | |
res.send(`You are posting an item with params: ${req.params}`); | |
}); | |
// PUT Method | |
router.put("/:id", (req, res) => { | |
res.send(`You are updating an item with id: ${req.params["id"]}`); | |
}); |
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
import express from "express"; | |
import { router as item_router } from "./items"; | |
import { router as user_router } from "./users"; | |
const app = express(); | |
const port = 3000; | |
// middleware | |
app.use(express.json()); // Process json body from requests | |
app.use(express.urlencoded({ extended: true })); // Process urlencoded payloads | |
app.use("/tdsvc/item", item_router); | |
app.use("/tdsvc/user", user_router); | |
app.listen(port, (err?) => { | |
if (err) { | |
return console.error(err); | |
} | |
return console.log(`server is listening on port: ${port}`); | |
}); |
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
import express from "express"; | |
export const router = express.Router(); | |
// GET Method | |
router.get("/:id", (req, res) => { | |
res.send(`You are requesting an user with id: ${req.params["id"]}`); | |
}); | |
// POST Method | |
router.post("/", (req, res) => { | |
res.send(`You are posting an user with params: ${req.params}`); | |
}); | |
// PUT Method | |
router.put("/:id", (req, res) => { | |
console.log(req); | |
res.send(`You are updating an user with id: ${req.params["id"]}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment