Implemented a CRUD application for managing users using Node.js and Express.
Attention: Make sure your server is running.
use:node NODE/server.jsto initialize the server.Note: if you get an error of Cross-Origin Request, make sure your server is configured to allow CORS (Cross-Origin Resource Sharing).
you can install by using:npm install cors
To start a Node.js project, you typically follow these steps:
npm init.
npm init -ynpm install.index.js) and start coding your server or application logic.npm i express : Installs the Express.js framework, which is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
In a Node.js project, server.js is commonly used as the main entry point for the application.
It typically contains the code to set up and start the server, define routes, and handle incoming requests.
to import modules in Node.js, you can use either the CommonJS syntax (require)
or the ES6 module syntax (import).
CommonJS:
const express = require('express');ES6 Modules:
import express from 'express';
Note: The ES6 module syntax is supported in Node.js starting from version 12 with the
.mjsfile extension
or by setting"type": "module"in thepackage.json.Starting the Server
// Using ES6 Modules import express from 'express'; const app = express(); const PORT = 3000; app.get('/', (req, res) => { res.send('Hello, World!'); }); app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });
HTML methods
HTML Request Methods
/users?name=John&age=30/users/:id{ "name": "John", "age": 30 }HTTP Status Codes
200 OK: The request was successful.201 Created: The request was successful and a resource was created.202 Accepted: The request has been received but not yet acted upon.400 Bad Request: The server could not understand the request due to invalid syntax.401 Unauthorized: The client must authenticate itself to get the requested response.403 Forbidden: The client does not have access rights to the content.404 Not Found: The server can not find the requested resource.500 Internal Server Error: The server has encountered a situation it doesn't know how to handle.502 Bad Gateway: The server, while acting as a gateway or proxy, received an invalid response from the upstream server.app.get(): Defines a route handler for GET requests to a specified path.app.post(): Defines a route handler for POST requests to a specified path.app.put(): Defines a route handler for PUT requests to a specified path.app.delete(): Defines a route handler for DELETE requests to a specified path.app.patch(): Defines a route handler for PATCH requests to a specified path.res.send(): Sends a response of various types (string, object, array, etc.) to the client.res.json(): Sends a JSON response to the client. It converts a JavaScript object or array into a JSON string and sets the appropriate content type.res.render(): Renders a view template and sends the resulting HTML to the client. This is commonly used with templating engines like EJS, Pug, or Handlebars.
// const express = require('express'); // CommonJS syntax
import express from 'express';
const app = express();
const PORT = 3000;
// see if the server is running
app.get('/', (req, res) => {
res.send('Hello, World!');
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
To start the server, you can use the following command in your terminal:
node NODE/server.js
Alternatively, you can use the following command to automatically restart the server whenever you make changes to the server.js file:
node --watch NODE/server.js
Query Params: Used to send data to the server as part of the URL, typically for filtering or searching.
Example: /users?name=John&age=30
Route Params: Used to capture values from the URL path, often for dynamic routing.
Example: /users/:id
Request Body: Used to send data to the server in the body of the request, often for creating or updating resources.
Example: { "name": "John", "age": 30 }
Query Params: Using req.query.variable, gets the value of the query parameter. Example: /example?variable=value
Route Params: Accessed using req.params in Express.js, which returns an object containing the route parameters defined in the URL path.
Request Body: Accessed using req.body in Express.js, which contains the data sent in the body of the request.
Note: To parse the request body, you need to use middleware like express.json() for JSON data or express.urlencoded() for URL-encoded data.
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate. It is commonly used for transmitting data in web applications between a server and a client.
Toon
Toon (Token Object Notation) is a compact, human-readable encoding of the JSON data model that minimizes tokens and makes structure easy for models to follow. It's intended for LLM input as a drop-in, lossless representation of your existing JSON.