SyntaxStudy
Sign Up
Home Node.js Reference http / https

http / https

property

Built-in modules for creating HTTP/HTTPS servers and making requests without third-party libraries.

Syntax

const http = require('http')

Example

javascript
const http = require('http');

// Create server
const server = http.createServer((req, res) => {
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify({ message: 'Hello World' }));
});

server.listen(3000, () => {
    console.log('Server running on http://localhost:3000');
});

// Make HTTP request
const https = require('https');
https.get('https://api.github.com/users/octocat', res => {
    let data = '';
    res.on('data', chunk => data += chunk);
    res.on('end', () => console.log(JSON.parse(data)));
});