Installing Express & Setting up the Server
Getting a server up and running with Express takes only a few lines of code. Let's initialize a Node.js project and configure our entry server file.
1 Project Initialization & Installation
First, initialize a new Node project and install Express from the package registry:
Shell — Terminal Setup
# Create project directory
mkdir express-app && cd express-app
# Initialize npm project package file
npm init -y
# Install the Express package
npm install express
2 Creating index.js Entry Point
JavaScript — index.js
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3000;
// Basic route entry point
app.get("/", (req, res) => {
res.send("Hello, World from Express!");
});
// Start the listener
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
3 Code Challenge
Challenge: Write a start command script inside your
package.json file, and run the server locally. Test the server locally in your browser at http://localhost:3000.