Answers for "create rest api node js express"

5

nodejs express api

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
Posted by: Guest on April-15-2021
2

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}`);
});
Posted by: Guest on August-04-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language