express()
- Node.js course
- Intermediate
- Lesson 25/116
- 5 min
Learn to create an app with express().
Creating an app with express()
The express() call creates an Express application (app). This application is the main object that manages routes and the server. Signature: const app = express();.
On the app object you define routes (app.get, app.post) and start the server (app.listen). Example: const express = require("express"); const app = express();. After this you access all Express features through app. express() returns a new, independent application object each time it is called, so in a project it is usually called once.
The app object
The app object is the heart of an Express application. On it you define routes, add middleware and start the server. All Express workflow revolves around app.
Example: const app = express(); creates the application. Then app.get("/", handler) adds a route. app.listen(3000) starts the server. app is an object; methods are called on it with a dot. The name is usually app but you can name it anything. What matters is that all route and server calls are on the same app object.
Practice Create an Express application
Import the express module. Create app with the express() call. Print "App ready" with console.log.
Starter code
// Import the express module
// write here
// Create app with the express call
// write here
// Print "App ready"
// write hereSample solution
const express = require("express");
const app = express();
console.log("App ready");Expected output
Uygulama hazır
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Complete the app creation line
const app = ____();
const app = express();
Explanation: The express() call gives the app object.
2 What does express() return?
const app = express()- An Express application (app) Correct answer
- Bir dize
- A number
- Bir http sunucusu
Explanation: express() returns an app object.
3 Match each method to its role
| Term | Meaning |
|---|---|
express() |
Creates the application |
app.get |
Defines a GET route |
app.post |
Defines a POST route |
app.listen |
Starts the server |