Supercharge your development with unmatched features:
Access a full terminal environment, run Linux commands, and manage your project’s dependencies directly within the IDE.
Browse and interact with websites directly within the IDE. Supports real-time interaction with web content without leaving the workspace.
Manage your project files and directories effortlessly within the IDE. Create, edit, rename, move, and delete files—all in one place.
Experience seamless code editing with real-time syntax highlighting, tab support, and intelligent code suggestions for a smoother development workflow.
Node.js is a JavaScript runtime built on Chrome's V8 engine, enabling server-side scripting with JavaScript. It is designed for building scalable network applications and is especially useful for I/O-heavy applications like web servers.
To get started with Node.js, download and install it from the official website: Node.js.
Verify the installation by running:
node -v
Create a simple Node.js application that listens to requests on a specified port:
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, Node.js!');
});
server.listen(3000, '127.0.0.1', () => {
console.log('Server running at http://127.0.0.1:3000/');
});
Run this file with:
node app.js
Node.js has a rich set of built-in modules that can be used for various tasks. For example, you can use the fs
module to interact with the file system:
const fs = require('fs');
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
Node.js provides asynchronous and synchronous methods for file operations. Here’s how to read a file asynchronously:
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
For synchronous operations:
const data = fs.readFileSync('example.txt', 'utf8');
console.log(data);
Node.js is commonly used to create HTTP servers. Here's a basic HTTP server example:
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
res.end('Hello from Node.js Server!
');
});
server.listen(3000, '127.0.0.1', () => {
console.log('Server running at http://127.0.0.1:3000/');
});
Express.js is a minimalist web framework for Node.js, often used to handle routing and HTTP methods. To get started with Express:
npm install express
Example of a simple Express server:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, Express!');
});
app.listen(3000, () => {
console.log('Server running at http://127.0.0.1:3000/');
});
Use Express to handle POST requests by adding the body-parser
middleware to parse the request body:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.post('/submit', (req, res) => {
const { name, age } = req.body;
res.send(`Received: ${name}, ${age}`);
});
app.listen(3000, () => {
console.log('Server running at http://127.0.0.1:3000/');
});
Middleware functions in Express are used to process requests before they reach the route handlers. Here's an example of custom middleware:
app.use((req, res, next) => {
console.log(`Request received at ${req.url}`);
next();
});
To run Node.js applications in production, it's important to use a process manager like PM2
:
npm install pm2 -g
pm2 start app.js --name "my-app"
pm2 save
PM2 will keep your application running even after a server restart and allows easy monitoring and management of your app.