Friday, May 28, 2021

create a node project and add basic routes using express

 To create a project, create a folder and right click on it and click on "open with vscode".


Then, in the terminal of vscode type below commands.


npm init --yes

npm i express


After that create index.js and add below code to it.

const express = require("express");

const app = express();

app.get("/", (reqres=> {
  res.send("Hello");
});

app.get("/api/courses", (reqres=> {
  res.send([123]); //courses array
});

app.listen(3000, () => console.log("listening"));

To run the code.

node index.js

In the browser, you can test the routes created.

http://localhost:3000/

http://localhost:3000/api/courses



No comments:

Post a Comment