Created
March 26, 2013 05:39
-
-
Save dasher/5243390 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Getting started | |
With node installed (download), get your first application started by creating a directory somewhere on your machine: | |
$ mkdir hello-world | |
$ cd hello-word | |
$ git init | |
In this same directory you'll be defining the application "package", which are no different than any other node package. You'll need a package.json file in the directory, with express defined as a dependency. You may use npm info express version to fetch the latest version, it's preferred that you do this instead of "3.x" below to prevent any future surprises. | |
{ | |
"name": "hello-world", | |
"description": "hello world test app", | |
"version": "0.0.1", | |
"private": true, | |
"dependencies": { | |
"express": "3.x" | |
} | |
} | |
Now that you have a package.json file in this directory you can use npm(1) to install the dependencies, in this case just Express: | |
$ npm install | |
Once npm finishes you'll have a localized Express 3.x dependency in the ./node_modules directory. You may verify this with npm ls as shown in the following snippet displaying a tree of Express and its own dependencies. | |
$ npm ls | |
[email protected] /private/tmp | |
└─┬ [email protected] | |
├── [email protected] | |
├─┬ [email protected] | |
│ ├── [email protected] | |
│ ├── [email protected] | |
│ ├── [email protected] | |
│ ├── [email protected] | |
│ └── [email protected] | |
├── [email protected] | |
├── [email protected] | |
├── [email protected] | |
├── [email protected] | |
├── [email protected] | |
├── [email protected] | |
├─┬ [email protected] | |
│ └── [email protected] | |
└─┬ [email protected] | |
└── [email protected] | |
Now to create the application itself! Create a file named server.js, require express and then create a new application with express(): | |
var express = require('express'); | |
var app = express(); | |
/* | |
With the new application instance you can start defining routes via app.VERB(), in this case "GET /" responding with the "Hello World" string. The req and res are the exact same objects that node provides to you, thus you may invoke res.pipe(), req.on('data', callback) and anything else you would do without Express involved. | |
*/ | |
app.get('/hello.txt', function(req, res){ | |
var body = 'Hello World'; | |
res.setHeader('Content-Type', 'text/plain'); | |
res.setHeader('Content-Length', body.length); | |
res.end(body); | |
}); | |
/* | |
Express augments these objects providing you with higher level methods such as res.send(), which among other things adds the Content-Length for you: | |
*/ | |
app.get('/hello.txt', function(req, res){ | |
res.send('Hello World'); | |
}); | |
/* | |
Now to bind and listen for connections invoke the app.listen() method, accepting the same arguments as node's net.Server#listen(): | |
*/ | |
app.listen(3000); | |
console.log('Listening on port 3000'); | |
Now save the file | |
$ git add . | |
$ git commit -m "My first app!" | |
$ node server.js | |
Now visit: "127.0.0.1:3000/hello.txt" in your browser :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment