Prerequicities: Toolchain installed (see 01-tools-env.md).
npm init inside the project folder. The wizard needs an interactive shell, use terminal on MacOS/Linux or Git Bash/Powershell on Windows.
type enter module: type: (commonjs) module)Setup ESLint and Prettier for code linting and formatting (Read more about the differences and roles of the tools on LogRocket Blog and ESLint blog).
# --save-exact makes sure that everyone in the project gets the exact same version of Prettier.
npm install --save-dev --save-exact prettier
npm install --save-dev eslint @eslint/js eslint-config-prettier globals
eslint.config.js file with the following content:import globals from 'globals';
import js from '@eslint/js';
export default [
{
languageOptions: {
ecmaVersion: 2021,
sourceType: 'module',
globals: {...globals.node},
},
},
js.configs.recommended,
];
.prettierrc.json file with the following content: {
"semi": true,
"singleQuote": true,
"bracketSpacing": false,
"trailingComma": "all"
}
nodemon as a development dependency: npm install --save-dev nodemon.
Review package.json, add a script for starting your app with nodemon, and add type property (if missing):
...
"type": "module",
"scripts": {
"dev": "nodemon src/index.js",
...
Initialize a git repository: git init and create a .gitignore file and add at least node_modules to it. Remember to keep the file always up to date when adding files you don’t want to include version control!
.vscode
node_modules
.DS_Store
Create a src/ folder and a file index.js in it
// index.js
import http from 'http';
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Welcome to my REST API!');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
.editorconfig file according to the example (see 01-tools-env.md)npm run dev (to stop the development server hit ctrl-c)Create a new local repository and setup a remote repository on GitHub and push your current local project to Github.
git init
git add .
git commit -m "Initial commit"
git remote add origin <YOUR-REPO-URL>
git push -u origin main
req variable is an request object containing information about the HTTP request that raised the event. e.g.:
req.url: the request URL stringreq.method: the HTTP request methodreq.headers: the request headersIn response to req, use response object’s (res) properties/methods to send the desired HTTP response back to the client. e.g.:
re.statusCode: set the response status coderes.write(): add content to response bodyres.end(): send te responsePayload data (request body) can be extracted manually from the request stream.
| Endpoint | Method | Description | Request Body (Example) | Response Body (Example) | Status Codes |
|---|---|---|---|---|---|
/items |
GET | Retrieve a list of all items | N/A | [{ "id": 1, "name": "Item1" }, { "id": 2, "name": "Item2" }] |
200 OK, 404 Not Found |
/items |
POST | Create a new item | { "name": "New Item" } |
{ "id": 3, "name": "New Item" } |
201 Created, 400 Bad Request |
/items/:id |
GET | Retrieve details of a specific item by its ID | N/A | { "id": 1, "name": "Item1" } |
200 OK, 404 Not Found |
/items/:id |
PUT | Update details of a specific item by its ID | { "name": "Updated Item" } |
{ "id": 1, "name": "Updated Item" } |
200 OK, 400 Bad Request, 404 Not Found |
/items/:id |
DELETE | Delete a specific item by its ID | N/A | N/A | 204 No Content, 404 Not Found |
This is a basic table for documentation. Depending on the complexity of the API, real-world documentation might also include:
Fast, unopinionated, minimalist web framework for Node.js
Express provides a robust web application feature set and many other popular frameworks are built on top of the Express framework.
npm install express
Express package is saved as a dependency in the project’s package.json file.
Node.js using built-in http module:
import http from 'http';
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Welcome to my REST API!');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Node with Express:
import express from 'express';
const hostname = '127.0.0.1';
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Welcome to my REST API!');
});
app.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
npx express-generator --view=pug pug-apppublic into your project folder and add any static files into it, e.g. html, css, js, images, etc.app.use(express.static('public'));
app.use('/static', express.static('public'));public folder at http://localhost:3000/filename when using root URL or http://localhost:3000/static/filename when using sub path// GET http://localhost:3000/api/resource
app.get('/api/resource', (req, res) => {
const myData = {title: 'This is an item', description: 'Just some dummy data here'};
res.json(myData);
});
Using route parameters (path variables, the preferred way in REST APIs):
// GET http://localhost:3000/api/resource/99
// property name (id) is set in the route definition
app.get('/api/resource/:id', (req, res) => {
console.log('path variables', req.params);
if (req.params.id === '99') {
const myData = {
title: 'This is a specific item, id: ' + req.params.id,
description: 'Just some dummy data here',
};
res.json(myData);
} else {
res.sendStatus(404);
}
});
Using query parameters:
// GET http://localhost:3000/api/resource?id=99&name=foo
app.get('/api/resource', (req, res) => {
if (req.query.id === '99') {
console.log('query params object', req.query);
const myData = {
title: 'This is a specific item, id: ' + req.query.id,
description: 'Just some dummy data here',
};
res.json(myData);
} else {
res.sendStatus(404);
}
});
// needed for reading request body in JSON format
app.use(express.json());
...
// POST http://localhost:3000/api/resource
// sends request data back to client
app.post('/api/resource', (req, res) => {
const body = req.body;
res.status(201);
res.json({your_request: body});
});
Follow the teacher’s example (link in Oma) and implement TODOs in the example code. See assignment instructions in Oma and submit there.
.gitignore and set a remote repo in Github, create and checkout a new branch git checkout -b week1git add . & git commit -m "describe you changes here") and push your week1 branch to the remote repo in Github (git push -u origin week1)