References: Express/Node introduction





NodeJs를 정상적으로 설치하였으면 간단한 코드로 NodeJs를 실행시켜 보겠습니다.




1. Workspace 생성


적당한 위치에 폴더를 하나 생성해 주세요.


e.g.  



2. js 파일 생성



NodeJs를 통해 Hello World를 출력하는 코드를 작성해 줍니다.


e.g) hello.js


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Load HTTP module
var http = require("http");
 
// Create HTTP server and listen on port 8000 for requests
http.createServer(function(request, response) {
 
   // Set the response HTTP header with HTTP status and Content type
   response.writeHead(200, {'Content-Type''text/plain'});
   
   // Send the response body "Hello World"
   response.end('Hello World\n');
}).listen(8000);
 
// Print URL for accessing server
console.log('Server running at http://127.0.0.1:8000/');
cs


3. 소스 실행.


CMD 또는 PowerShell을 실행후 workspace 폴더로 위치를 변경합니다.


node hello.js 를 입력합니다.




4. 결과 확인


다음과 같은 결과를 확인합니다.



반응형

+ Recent posts