java-script:node-js:rest-api
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| java-script:node-js:rest-api [2023/08/09 17:11] – removed - external edit (Unknown date) 127.0.0.1 | java-script:node-js:rest-api [2025/01/02 18:22] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Simple REST Api backend implementation using Node.js and Express.js ====== | ||
| + | ===== Project implementation ===== | ||
| + | |||
| + | First, [[java-script: | ||
| + | Then, install [[java-script: | ||
| + | < | ||
| + | npm install express | ||
| + | </ | ||
| + | |||
| + | Create a src folder, inside it we'll place the following files. | ||
| + | |||
| + | ==== Simple Hello World rest service ==== | ||
| + | |||
| + | <code javascript node-rest-express-hello.js> | ||
| + | import express from " | ||
| + | |||
| + | let port = 3000; | ||
| + | |||
| + | let app = express(); | ||
| + | |||
| + | //define endpoints | ||
| + | |||
| + | app.get('/ | ||
| + | response.send(" | ||
| + | }) | ||
| + | |||
| + | app.listen(port, | ||
| + | console.log(`App listening on port ${port}`); | ||
| + | }); | ||
| + | </ | ||
| + | |||
| + | To start the server: | ||
| + | < | ||
| + | node src/ | ||
| + | </ | ||
| + | |||
| + | To access the /hello endpoint, open in a browser: http:// | ||
| + | Hello World! will be displayed. | ||
| + | |||
| + | ==== Simple GET, GET by an attribute, POST ==== | ||
| + | |||
| + | Data is loaded from person.js here but in a real-world application usually it is loaded from a database like mongodb. | ||
| + | |||
| + | <code javascript people.js> | ||
| + | export let people = [{ | ||
| + | name: " | ||
| + | age: 35, | ||
| + | hairColor: " | ||
| + | }, { | ||
| + | name: " | ||
| + | age: 20, | ||
| + | hairColor: " | ||
| + | }, { | ||
| + | name: " | ||
| + | age: 40, | ||
| + | hairColor: " | ||
| + | }]; | ||
| + | </ | ||
| + | |||
| + | <code javascript node-rest-express-people.js> | ||
| + | import express from " | ||
| + | import { people } from ' | ||
| + | |||
| + | let port = 3000; | ||
| + | |||
| + | let app = express(); | ||
| + | |||
| + | // used for the POST endpoint | ||
| + | // takes the extra data that the client sends along with the request and puts it on the request argument of the POST endpoint. | ||
| + | app.use(express.json()); | ||
| + | app.use(express.urlencoded({ extended: true })); // for parsing application/ | ||
| + | |||
| + | //define endpoints | ||
| + | |||
| + | app.get('/ | ||
| + | response.json(people); | ||
| + | }) | ||
| + | |||
| + | app.get('/ | ||
| + | | ||
| + | let { name } = request.params; | ||
| + | let person = people.find(x => x.name === name); | ||
| + | | ||
| + | response.json(person); | ||
| + | }) | ||
| + | |||
| + | app.post('/ | ||
| + | | ||
| + | let newPerson = request.body; | ||
| + | people.push(newPerson); | ||
| + | | ||
| + | response.json(people); | ||
| + | }) | ||
| + | |||
| + | app.listen(port, | ||
| + | console.log(`App listening on port ${port}`); | ||
| + | }); | ||
| + | </ | ||
| + | |||
| + | === Test the /people POST endpoint === | ||
| + | |||
| + | <note warning> | ||
| + | By default, web browsers use the GET method when you enter a URL in the address bar. This means you can't directly send a POST request using the address bar of a browser. | ||
| + | </ | ||
| + | |||
| + | To test the POST endpoint we can use Postman, but there [[java-script: | ||
| + | |||
| + | After defining the body request, hit SEND button. \\ | ||
| + | Here is how the postman POST request and response will look like: \\ | ||
| + | {{: | ||
| + | |||
| + | ==== Load data from a file (server side) ==== | ||
| + | |||
| + | We'll use now people-data.json to load the people data. | ||
| + | |||
| + | <code javascript people-data.json> | ||
| + | [{ | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | }, { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | }, { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | }] | ||
| + | </ | ||
| + | |||
| + | We'll define a GET endpoint at /file-data. When a GET request is made to this endpoint, the server reads data from a JSON file (people-data.json), | ||
| + | |||
| + | <code javascript node-rest-express-load-data.js> | ||
| + | import express from ' | ||
| + | import { promises as fs } from ' | ||
| + | import { fileURLToPath } from ' | ||
| + | import { dirname } from ' | ||
| + | |||
| + | const __filename = fileURLToPath(import.meta.url); | ||
| + | const __dirname = dirname(__filename); | ||
| + | |||
| + | let port = 3000; | ||
| + | |||
| + | let app = express(); | ||
| + | |||
| + | //define endpoints | ||
| + | |||
| + | app.get('/ | ||
| + | let data = await fs.readFile(__dirname + '/ | ||
| + | let people = JSON.parse(data); | ||
| + | |||
| + | response.json(people); | ||
| + | }) | ||
| + | |||
| + | app.listen(port, | ||
| + | console.log(`App listening on port ${port}`); | ||
| + | }); | ||
| + | </ | ||
| + | |||
| + | Result will be available at http:// | ||
