nodeJS rest api
// My dependencies
const express = require("express");
const bodyParser = require("body-parser");
// App configurations
const app = express();
// Let the app be able to see the html file that called the server
app.use(bodyParser.urlencoded({ extended: true }));
// A RESTFul GET API
app.get("/", (req, res) => {
res.send("Hello World");
});
// Another RESTFul GET API
app.get("/api/course", (req, res) => {
res.send([1,2,3]);
});
// My parameters input RESTFul API
app.get("/test/:year/:month", (req, res) => {
// http://localhost:55555/test/2020/5
res.send(req.params);
});
// My parameters input POST RESTFul API
app.post("/test2/:year/:month", (req, res) => {
// Same as the above one but with POST method
res.send(req.params);
});
// Starting up the server
const port = 55555;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});