Express.js
Express.js, or simply Express, is a back end web application framework for building RESTful APIs with Node.js, released as free and open-source software under the MIT License. It is designed for building web applications and APIs. It has been called the de facto standard server framework for Node.js.
The original author, TJ Holowaychuk, described it as a Sinatra-inspired server, meaning that it is relatively minimal with many features available as plugins. Express is the back-end component of popular development stacks like the MEAN, MERN or MEVN stack, together with the MongoDB database software and a JavaScript front-end framework or library.
Getting started
The Pro Linux Container of Brap is already pre-installed for Express.js.
Create a directory to hold your application, and make that your working directory.
mkdir my-expressjs-app
cd my-expressjs-app
Use the npm init
command to create a package.json
file for your application:
npm init
This command prompts you for a number of things, such as the name and version of your application. For now, you can simply hit RETURN to accept the defaults for most of them, with the following exception:
entry point: (index.js)
Enter app.js, or whatever you want the name of the main file to be. If you want it to be index.js, hit RETURN to accept the suggested default file name.
Now install Express in the myapp directory and save it in the dependencies list. For example:
npm install express
Hello, World!
Here is a Hello, World!
application in Express.js:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
To run the application. run:
node index.js
You should then be able to access the application on fun-sample-3000.brap.dev
, for example.
Screenshot: Express.js running in the terminal
Screenshot: Express.js running in the web browser
Keywords
- express
- javascript
- web
- framework
- mvc
- backend
- frontend
- fullstack