PROGRAMMING/node.js

[node.js] http 서버

seulda 2021. 4. 7. 17:38
728x90

 

 

const PORT = 8080;
const HTML_FILE = './server.html';
const http = require('http');
const fs = require('fs');

const server = http.createServer((req, res) => {
    fs.readFile(HTML_FILE, (error, data) => {
        if (error) {
            throw Error('파일이 없어요');
        }
        res.end(data);
    });  
    console.log('HiHi');
});
server.listen(PORT, () => {
    console.log('8000포트 대기중');
});

 

서버에 접속시에 console에 'HiHi'가 띄워진다.

 

 

728x90