Node.js: Difference between revisions

From 탱이의 잡동사니
Jump to navigation Jump to search
(Created page with "== Overview == Node.js 정보 정리 == Installation == === Ubuntu === <pre> $ sudo apt-get install nodejs-legacy $ sudo apt-get install npm </pre> Ubuntu 에서 node.js 설...")
 
Line 28: Line 28:
== Hello world ==
== Hello world ==


<source lang=js>
var express = require('express');
var app = express();
app.get('/', function(req, res){
    res.send('Hello world!');
})
var server = app.listen(3000, function() {
    var host = server.address().address;
    var port = server.address().port;
   
    console.log('Example app listening at http://%s:%s', host, port);
});
</source>
다음과 같이 실행한다.
<pre>
$ node app.js
</pre>
다음과 같이 테스트하면 된다.
<pre>
$ curl localhost:3000
Hello world!
</pre>


== See also ==
== See also ==

Revision as of 18:03, 31 August 2015

Overview

Node.js 정보 정리

Installation

Ubuntu

$ sudo apt-get install nodejs-legacy
$ sudo apt-get install npm

Ubuntu 에서 node.js 설치시, 설치가능한 패키지가 두개가 있다. 보통은 nodejs-legacy 를 설치하면 된다<ref>http://stackoverflow.com/questions/20057790/what-are-the-differences-between-node-js-and-node</ref>.

$ node -v
The program 'node' can be found in the following packages:
 * node
 * nodejs-legacy
Try: sudo apt-get install <selected package>

Install packages

node.js 에서 사용하는 패키지를 설치하기 위해서는 npm 명령어를 사용하면 된다.

예를 들어, 아래 hello world 예제에서 사용하는 express 패키지를 설치하기 위해서는 다음을 입력하면 된다.

$ sudo npm install express

Hello world

<source lang=js> var express = require('express'); var app = express();

app.get('/', function(req, res){

   res.send('Hello world!');

})

var server = app.listen(3000, function() {

   var host = server.address().address;
   var port = server.address().port;
   
   console.log('Example app listening at http://%s:%s', host, port);

}); </source>

다음과 같이 실행한다.

$ node app.js 

다음과 같이 테스트하면 된다.

$ curl localhost:3000
Hello world!

See also

References

<references />