📅  最后修改于: 2023-12-03 15:17:36.159000             🧑  作者: Mango
REST (Representational State Transfer) is an architectural style for building distributed systems. It is frequently used in web applications to exchange data between the client and server.
REST is a set of architectural constraints that can be used to design a web service. RESTful web services adhere to these constraints, making them highly scalable and maintainable.
Express is a popular Node.js framework for building web applications, including REST APIs.
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
app.use(express.static('public'));
app.get('/users', (req, res) => {
const users = [{ name: 'John', age: 25 }, { name: 'Jane', age: 30 }];
res.json(users);
});
app.post('/users', (req, res) => {
// Handle creation of new user
});
app.put('/users/:id', (req, res) => {
// Handle updating user with given ID
});
app.delete('/users/:id', (req, res) => {
// Handle deletion of user with given ID
});
RESTful web services provide a flexible and scalable approach to building distributed systems. With the power of JavaScript, we can easily build a RESTful API using frameworks like Express. Keep the key principles of REST in mind while designing your API and you'll be on your way to building a highly maintainable and scalable application.