Get started with Node.js for building fast and scalable server-side applications with JavaScript.
By Upingi Team / Tutorial Level: Beginner
Why Learn Node.js?
Node.js is a JavaScript runtime environment that allows you to run JavaScript code outside of a web browser. It's widely used for building backend APIs, microservices, real-time applications, and command-line tools.
Leveraging your existing JavaScript knowledge for backend development is a major advantage.
Prerequisites
- Basic JavaScript Knowledge: Understanding variables, functions, data types, and asynchronous concepts (callbacks, Promises).
- Install Node.js and npm: Download and install the LTS (Long Term Support) version from the official Node.js website. npm (Node Package Manager) is included.
Let's build a simple server!
Chapter 1: Your First Node.js Script
Learn how to run Node.js scripts and use its built-in module system.
- Create a script: Make a file named `app.js` and add `console.log("Hello from Node.js!");`.
- Run the script: Open your terminal and run `node app.js`.
- Modules: Node.js uses the CommonJS module system. Use `require` to import built-in modules (like `fs` for file system or `http` for server): `const fs = require('fs');`.
- Creating Modules: Create another file (`myModule.js`), define a function or variable, and export it using `module.exports = myFunction;` or `module.exports = { key: value };`.
- Importing Custom Modules: Use `require` with the file path: `const myModule = require('./myModule');`.
You can now execute Node.js code and organize it into modules.
Chapter 2: npm & Simple Server
npm (Node Package Manager) is used to manage third-party packages (libraries/modules) for your Node.js projects.
- Initialize a Project: Navigate to your project directory in the terminal and run `npm init -y`. This creates a `package.json` file, which tracks project metadata and dependencies. The `-y` flag accepts default settings.
- Install Packages: Use `npm install ` to add a dependency. For example, `npm install express` installs the popular Express web framework. This downloads the package into a `node_modules` folder and adds it to your `package.json` and `package-lock.json` (which ensures consistent installs).
- Basic HTTP Server: Node.js has a built-in `http` module for creating servers. Create a file (e.g., `server.js`):
const http = require('http');
const hostname = '127.0.0.1'; // Localhost
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World from Node.js Server!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Run this file using `node server.js` and visit `http://127.0.0.1:3000` in your browser. You've created a basic web server!
Conclusion & Next Steps
You've learned the basics of running Node.js scripts, using modules, managing packages with npm, and the concept of creating a simple server. This opens the door to backend development with JavaScript.
Where to go next:
- Explore the Express.js framework for building web applications and APIs more easily.
- Learn about asynchronous JavaScript in Node.js (callbacks, Promises, async/await).
- Understand routing, middleware, and request/response handling in web frameworks.
- Connect to databases (e.g., MongoDB, PostgreSQL) from Node.js.
- Explore other Node.js modules and frameworks.