1.node.js설치
https://nodejs.org/en/ 에서 다운로드 받을수 있습니다.
2.버전확인
node --version
3.원하는 폴더생성
4.$npm init
(처음 이름만 써주고 다 기본값설정 엔터누르면됨)
package.json파일 생성
프로젝트 정보기록파일 (npm으로 프로젝트 관리하기위해 사용)
5.js파일 생성
nodejs.org 에 예제샘플이 있습니다.
소스
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
위와같은 소스를 새로만들어서 example.js로 저장
6. npm으로 start 스크립트생성 (실행하기편하게)
package.json파일을 에디터로 열어서
"scripts": {
} 안쪽에
"start": "node example.js"
를 적어줍니다. 안에뭐가있다면 ,로 구분해주세요.
7.서버시작
npm start 누르면 서버가 시작됩니다.
8.브라우저로 확인
127.0.0.1:3000 번으로 가면 확인할수 있습니다.