Start our first Express project, Hello Express! To practice our very basic skills building simple REST API with Node.js
- Various programs we built before
- Install Express on your project -
-
REST API that can handle basic HTTP methods
- GET
- POST
- PUT
- DELETE
- Initialize your node project first
npm init
- Always remember to ignore
node_modules
echo node_modules > .gitignore
- Install Express using
npm
oryarn
# using npm
npm i express
# using yarn
yarn add express
- Create a basic Express app in
index.js
const express = require("express")
const app = express()
app.get("/", function (req, res) {
res.send("Hello World!")
})
app.listen(3000, function () {
console.log("Example app listening on port 3000!")
})
- Create a
start
script
inpackage.json
{
...
"scripts": {
"start": "node index"
}
...
}
- Test if that works
npm start
- Awesome!
- Read the documentation on Express
- Start to implement various REST API routes/requests:
GET
,POST
,PUT
,DELETE
- Congrats!
- Compare between
res.send
/res.json
andres.render
- Response with either JSON or webpage
- Explore with Express application generator