P5.1: Node.js & Express Basics
Until now, we have written JavaScript code that runs exclusively inside the browser DOM. Node.js allows us to run JavaScript on our local computer or backend servers, opening the door to full-stack development!
Until now, we have written JavaScript code that runs exclusively inside the browser DOM. Node.js allows us to run JavaScript on our local computer or backend servers, opening the door to full-stack development!
Lesson: p5-1-node
Until now, we have written JavaScript code that runs exclusively inside the browser DOM. Node.js allows us to run JavaScript on our local computer or backend servers, opening the door to full-stack development!
Node.js is an open-source, cross-platform JavaScript runtime environment built on Google Chrome's V8 engine. It executes JavaScript code outside of a web browser.
window, document, or alert(). Instead, you gain access to server APIs like fs (File System) and path.NPM (Node Package Manager) is the default package manager for Node.js. It allows you to download and manage third-party libraries (packages) for your project.
To start a new Node.js project, run this command in your terminal:
npm init -y
This generates a package.json file, which tracks your project's dependencies and configuration metadata.
Express is a minimal and flexible web application framework for Node.js, providing a robust set of features for building web servers and APIs.
Run the install command in your project folder:
npm install express
server.js)Create a file named server.js and write the setup:
const express = require('express');
const app = express();
const PORT = 5000;
// Define a simple root route
app.get('/', (req, res) => {
res.send('Hello from the Node.js Server!');
});
// Start the server listening on PORT
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
});
Start the script in your terminal:
node server.js
Open http://localhost:5000 in your web browser, and you will see your server response!
The Node.js event loop handles operations asynchronously, making it highly efficient for I/O-intensive web servers.
Number of requests handled per second under high I/O loads.