상세 컨텐츠

본문 제목

[Node.js] Http Routing

Node.json

by simstealer 2024. 11. 8. 18:18

본문

const http = require('http');
const server = http.createServer((req, res) => {
    if (req.url === '/home') // http//localhost:3000/home
    {
        res.writeHead(200, {'Content-Type': 'application/json'});
        res.end(JSON.stringify({a: "a", b: "b"}));
    }
    else if (req.url === '/about') // http//localhost:3000/about
    {
        res.setHeader('Content-Type', 'text/html');
        res.write('<html>');
        res.write('<body>');
        res.write('<h1>About Page</h1>');
        res.write('</body>');
        res.write('</html>');
    }
    else // http//localhost:3000/그 외 페이지
    {
        res.statusCode = 404;
        res.end('Not Found');
    }

});

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

'Node.json' 카테고리의 다른 글

[Node.js] POST로 data 추가하기  (0) 2024.11.08
[Node.js] Web Server Create  (0) 2024.11.08
NPM  (0) 2022.09.15
cluster  (0) 2022.09.15
https/http2  (0) 2022.09.15

관련글 더보기

댓글 영역