java-script:node-js:express-js
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| java-script:node-js:express-js [2023/08/09 17:11] – removed - external edit (Unknown date) 127.0.0.1 | java-script:node-js:express-js [2025/01/02 18:22] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Node.js use Express.js ====== | ||
| + | ===== About ===== | ||
| + | |||
| + | Express.js, or simply Express, is a web application framework for Node.js. It was designed for building web applications and APIs. Express is known for its simplicity, flexibility, | ||
| + | |||
| + | We'll use **Express.js** as a web server implementation for Node.js. It simplifies many of the complexities of setting up a server in Node.js. \\ | ||
| + | Express provides mechanisms to: | ||
| + | * handle routing | ||
| + | * handle request and response objects | ||
| + | * handle middleware | ||
| + | * several other features which make it easier to build web apps and APIs on top of Node.js. | ||
| + | |||
| + | < | ||
| + | Express.js does not replace the built-in HTTP server in Node.js, but rather works on top of it, providing a higher-level, | ||
| + | </ | ||
| + | |||
| + | <note tip> | ||
| + | While Express ;;;can serve static files (like HTML, CSS, and JavaScript files);;;, it's often used as an ;;;API server that communicates with a front-end application built with a library or framework like React, Angular, or Vue.js;;;. This allows you to create full-stack JavaScript applications where both the front-end and the back-end are written in JavaScript. \\ \\ | ||
| + | |||
| + | Express.js is not the only option for building web servers in Node.js. Other popular options include Koa.js, Hapi.js, and Fastify, each with their own strengths and use cases. | ||
| + | </ | ||
| + | |||
| + | ===== Installation ===== | ||
| + | |||
| + | You must have Node.js and npm (Node Package Manager) installed on your computer to use Express.js. | ||
| + | You would then install Express with the command | ||
| + | < | ||
| + | npm install express | ||
| + | </ | ||
| + | |||
| + | ===== Very basic example ===== | ||
| + | |||
| + | <code javascript express.js> | ||
| + | const express = require(' | ||
| + | const app = express(); | ||
| + | const port = 3000; | ||
| + | |||
| + | app.get('/', | ||
| + | res.send(' | ||
| + | }); | ||
| + | |||
| + | app.listen(port, | ||
| + | console.log(`Example app listening at http:// | ||
| + | }); | ||
| + | |||
| + | </ | ||
