0% found this document useful (0 votes)
40 views3 pages

Express.js REST API Cheat Sheet

This cheat sheet provides a quick reference for setting up a Node.js and Express.js application with REST API routes. It includes examples for handling GET, POST, PUT, and DELETE requests, along with common HTTP status codes and how to access request parameters and query strings. The document serves as a guide for developers to efficiently implement RESTful services using Express.js.

Uploaded by

mhatredeep27
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

Express.js REST API Cheat Sheet

This cheat sheet provides a quick reference for setting up a Node.js and Express.js application with REST API routes. It includes examples for handling GET, POST, PUT, and DELETE requests, along with common HTTP status codes and how to access request parameters and query strings. The document serves as a guide for developers to efficiently implement RESTful services using Express.js.

Uploaded by

mhatredeep27
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[Link] + Express.

js + REST API Cheat Sheet

1. Setup Express App

const express = require('express');

const app = express();

const PORT = 3000;

[Link]([Link]()); // Middleware to parse JSON

[Link](PORT, () => [Link](`Server running on [Link]

2. Routes & REST Methods

// GET - Fetch all users

[Link]('/users', (req, res) => {

[Link]([{ id: 1, name: 'Deep' }]);

});

// POST - Create user

[Link]('/users', (req, res) => {

const { name } = [Link];

[Link](201).json({ message: `User ${name} created` });

});

// PUT - Update user

[Link]('/users/:id', (req, res) => {

[Link](`User ${[Link]} updated`);


});

// DELETE - Delete user

[Link]('/users/:id', (req, res) => {

[Link](`User ${[Link]} deleted`);

});

3. Request Types

GET - Retrieve data

POST - Create new data

PUT - Update entire object

PATCH - Update part of object

DELETE - Remove data

4. Status Codes

200 - OK

201 - Created

400 - Bad Request

401 - Unauthorized

404 - Not Found

500 - Server Error

5. Request Params & Query

// Params: /user/:id

[Link]

// Query: /search?name=deep
[Link]

You might also like