Node.json

[Node.js] Web Server Create

simstealer 2024. 11. 8. 18:05

 

// Text
const http = require('http');
const server = http.createServer((req, res) => {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(`
        <html>
            <head>
                <title>Server Response</title>
            </head>
            <body>
                <h1>Server Response</h1>
                <p>This is a server response.</p>
            </body>
        </html>
    `);
});

server.listen(3000, () => {
    console.log('Server is running on port 3000');
});

// JavaScripts Object
const http = require('http');
const server = http.createServer((req, res) => {
    res.writeHead(200, {'Content-Type': 'application/json'});
    res.end(JSON.stringify({
        a: "a",
        b: "b"
    }));
});

server.listen(3000, () => {
    console.log('Server is running on port 3000');
});