Statelessness means the server does not keep client context between requests. This makes scaling easier because any server can handle any request without needing previous information.
Using POST for all actions prevents caching because POST requests are not cached by default. This can slow down the API and increase server load.
const express = require('express'); const app = express(); const users = { 42: { id: 42, name: 'Alice' } }; app.get('/users/:id', (req, res) => { const user = users[req.params.id]; if (!user) { return res.status(404).json({ error: 'User not found' }); } res.json(user); });
The route finds the user with id 42 and returns it as JSON with status 200. The id is a number, so the JSON reflects that.
RESTful APIs use the DELETE method on the resource URL to delete it. Using POST, GET, or PUT for delete actions breaks REST conventions.
const express = require('express'); const app = express(); let sessionData = {}; app.post('/login', (req, res) => { const userId = req.body.userId; sessionData[userId] = { loggedIn: true }; res.json({ message: 'Logged in' }); }); app.get('/profile', (req, res) => { const userId = req.query.userId; if (sessionData[userId]?.loggedIn) { res.json({ profile: 'User profile data' }); } else { res.status(401).json({ error: 'Not logged in' }); } }); app.listen(3000);
REST requires stateless servers. Storing session data in memory means requests depend on server memory, so scaling with multiple servers fails because session data is not shared.