Initial commit
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
node_modules
|
||||||
|
.DS_Store
|
||||||
|
api.html
|
||||||
|
index.html
|
||||||
19
Makefile
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
|
||||||
|
JADE = ./node_modules/.bin/jade
|
||||||
|
|
||||||
|
HTML = index.html \
|
||||||
|
api.html \
|
||||||
|
guide.html \
|
||||||
|
applications.html \
|
||||||
|
community.html \
|
||||||
|
faq.html
|
||||||
|
|
||||||
|
docs: $(HTML)
|
||||||
|
|
||||||
|
%.html: %.jade
|
||||||
|
$(JADE) --path $< < $< > $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f *.html
|
||||||
|
|
||||||
|
.PHONY: docs clean
|
||||||
26
Readme.md
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
# ExpressJS.com
|
||||||
|
|
||||||
|
The site for Express.
|
||||||
|
|
||||||
|
## Building
|
||||||
|
|
||||||
|
Setup:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ npm install -g serve
|
||||||
|
$ npm install
|
||||||
|
$ make
|
||||||
|
$ serve . &
|
||||||
|
$ open http://localhost:3000
|
||||||
|
```
|
||||||
|
|
||||||
|
then rebuild changes with:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ make
|
||||||
|
```
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
- __dont__ edit the HTML directly, edit the _jade_.
|
||||||
33
api.jade
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
!!! 5
|
||||||
|
html
|
||||||
|
head
|
||||||
|
title Express - api reference
|
||||||
|
include includes/head
|
||||||
|
body.inner
|
||||||
|
.bar
|
||||||
|
section#content
|
||||||
|
header
|
||||||
|
include includes/logo
|
||||||
|
active = '/api.html'
|
||||||
|
include includes/menu
|
||||||
|
|
||||||
|
include includes/mixins
|
||||||
|
|
||||||
|
include en/api/menu
|
||||||
|
|
||||||
|
#right
|
||||||
|
include en/api/express
|
||||||
|
|
||||||
|
h2 Application
|
||||||
|
a(name='application')
|
||||||
|
include en/api/app
|
||||||
|
|
||||||
|
h2 Request
|
||||||
|
a(name='request')
|
||||||
|
include en/api/req
|
||||||
|
|
||||||
|
h2 Response
|
||||||
|
a(name='response')
|
||||||
|
include en/api/res
|
||||||
|
|
||||||
|
include includes/footer
|
||||||
93
app.js
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
|
||||||
|
o = $;
|
||||||
|
|
||||||
|
// misc junk
|
||||||
|
|
||||||
|
o(function(){
|
||||||
|
var width = window.innerWidth;
|
||||||
|
var height = window.innerHeight;
|
||||||
|
var doc = o(document);
|
||||||
|
|
||||||
|
// .onload
|
||||||
|
o('html').addClass('onload');
|
||||||
|
|
||||||
|
// top link
|
||||||
|
o('#top').click(function(e){
|
||||||
|
o('body').animate({ scrollTop: 0 }, 'fast');
|
||||||
|
e.preventDefault();
|
||||||
|
});
|
||||||
|
|
||||||
|
// scrolling links
|
||||||
|
var added;
|
||||||
|
doc.scroll(function(e){
|
||||||
|
if (doc.scrollTop() > 5) {
|
||||||
|
if (added) return;
|
||||||
|
added = true;
|
||||||
|
o('body').addClass('scroll');
|
||||||
|
} else {
|
||||||
|
o('body').removeClass('scroll');
|
||||||
|
added = false;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// highlight code
|
||||||
|
o('pre.js code').each(function(){
|
||||||
|
o(this).html(highlight(o(this).text()));
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// active menu junk
|
||||||
|
|
||||||
|
o(function(){
|
||||||
|
var prev;
|
||||||
|
var n = 0;
|
||||||
|
|
||||||
|
var headings = o('h3').map(function(i, el){
|
||||||
|
return {
|
||||||
|
top: o(el).offset().top,
|
||||||
|
id: el.id
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function closest() {
|
||||||
|
var h;
|
||||||
|
var top = o(window).scrollTop();
|
||||||
|
var i = headings.length;
|
||||||
|
while (i--) {
|
||||||
|
h = headings[i];
|
||||||
|
if (top >= h.top) return h;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
o(document).scroll(function(){
|
||||||
|
var h = closest();
|
||||||
|
if (!h) return;
|
||||||
|
|
||||||
|
if (prev) {
|
||||||
|
prev.removeClass('active');
|
||||||
|
prev.parent().parent().removeClass('active');
|
||||||
|
}
|
||||||
|
|
||||||
|
var a = o('a[href="#' + h.id + '"]');
|
||||||
|
a.addClass('active');
|
||||||
|
a.parent().parent().addClass('active');
|
||||||
|
|
||||||
|
prev = a;
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Highlight the given `js`.
|
||||||
|
*/
|
||||||
|
|
||||||
|
function highlight(js) {
|
||||||
|
return js
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
.replace(/\/\/(.*)/gm, '<span class="comment">//$1</span>')
|
||||||
|
.replace(/('.*?')/gm, '<span class="string">$1</span>')
|
||||||
|
.replace(/(\d+\.\d+)/gm, '<span class="number">$1</span>')
|
||||||
|
.replace(/(\d+)/gm, '<span class="number">$1</span>')
|
||||||
|
.replace(/\bnew *(\w+)/gm, '<span class="keyword">new</span> <span class="init">$1</span>')
|
||||||
|
.replace(/\b(function|new|throw|return|var|if|else)\b/gm, '<span class="keyword">$1</span>')
|
||||||
|
}
|
||||||
25
applications.html
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
<!DOCTYPE html><html><head><title>Express - applications</title><link rel="stylesheet" href="style.css"><link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700&subset=latin,latin-ext"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script><script src="app.js"></script></head><body class="applications inner"><div class="bar"></div><section id="content"><header><section id="logo"><span class="express">express<em>3.0.0</em></span><span class="description">
|
||||||
|
web application framework for <a href="http://nodejs.org">node </a></span></section><nav class="clearfix"><a href="/" class=""> Home</a><a href="/api.html" class=""> API Reference</a><a href="/guide.html" class=""> Guide</a><a href="/applications.html" class="active"> Applications</a><a href="/community.html" class=""> Community</a><a href="/faq.html" class=""> FAQ</a></nav><div id="x"></div><div id="y"></div></header><section class="application"><h2>LearnBoost</h2><p>LearnBoost provides a free, easy to use online
|
||||||
|
online education suite including gradebook,
|
||||||
|
lesson plans, attendance, reporting, and calendars
|
||||||
|
among other tools.
|
||||||
|
</p><div class="link">Visit <a href="https://www.learnboost.com/">LearnBoost</a></div><img src="/images/apps/screenshots/learnboost.small.png"></section><section class="application"><h2>Storify</h2><p>Create stories using social media. Turn what people post
|
||||||
|
on social media into compelling stories. Collect the best photos, video,
|
||||||
|
tweets and more to publish
|
||||||
|
</p><div class="link">Visit <a href="http://storify.com/">Storify</a></div><img src="/images/apps/screenshots/storify.small.png"></section><section class="application"><h2>Geekli.st</h2><p>A place for geeks to share what they've done, who they did it with and
|
||||||
|
connect with great companies and communities.
|
||||||
|
</p><div class="link">Visit <a href="http://geekli.st">Geekli.st</a></div><img src="/images/apps/screenshots/geeklist.small.png"></section><section class="application"><h2>Klout</h2><p>Klout is the Standard for Influence. Join Klout to discover your
|
||||||
|
influence and compare with others you may know.
|
||||||
|
</p><div class="link">Visit <a href="http://klout.com">Klout</a></div><img src="/images/apps/screenshots/klout.small.png"></section><section class="application"><h2>Prismatic</h2><p>Prismatic learns from how you interact on social networks so that we
|
||||||
|
can show you the most interesting content and conversation from your friends.
|
||||||
|
</p><div class="link">Visit <a href="http://getprismatic.com/">Prismatic</a></div><img src="/images/apps/screenshots/prismatic.small.png"></section><section class="application"><h2>Clipboard</h2><p>Clipboard is a powerful tool allowing to save live clips
|
||||||
|
of your interests on the web, not just static images,
|
||||||
|
but fully-functional fragments of anything online.
|
||||||
|
</p><div class="link">Visit <a href="http://clipboard.com/">Clipboard</a></div><img src="/images/apps/screenshots/clipboard.small.png"></section><section class="application"><h2>Persona</h2><p>Persona, or "BrowserID" is Mozilla's answer
|
||||||
|
to a better identification system for your browser,
|
||||||
|
this promising tool is definitely worth checking out.
|
||||||
|
</p><div class="link">Visit <a href="https://login.persona.org/">Persona</a></div><img src="/images/apps/screenshots/browserid.small.png"></section><section class="application"><h2>and more!</h2><p>Shodan search reports that there are well over <strong>26,000</strong> Express applications
|
||||||
|
in the wild, we can't possibly list them all here, but if you feel
|
||||||
|
your application helps showcase the framework open an issue on
|
||||||
|
the <a href="github.com/visionmedia/expressjs.com/issues">github repo</a>.
|
||||||
|
</p><img src="/images/apps/screenshots/more.small.png"></section></section><a id="top" href="#"><img src="images/arrow.png"></a><footer><div id="footer-content">© 2012 TJ Holowaychuk. All rights reserved.</div></footer></body></html>
|
||||||
14
applications.jade
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
!!! 5
|
||||||
|
html
|
||||||
|
head
|
||||||
|
title Express - applications
|
||||||
|
include includes/head
|
||||||
|
body.applications.inner
|
||||||
|
.bar
|
||||||
|
section#content
|
||||||
|
header
|
||||||
|
include includes/logo
|
||||||
|
active = '/applications.html'
|
||||||
|
include includes/menu
|
||||||
|
include en/applications
|
||||||
|
include includes/footer
|
||||||
15
community.html
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<!DOCTYPE html><html><head><title>Express - community</title><link rel="stylesheet" href="style.css"><link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700&subset=latin,latin-ext"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script><script src="app.js"></script></head><body class="community inner"><div class="bar"></div><section id="content"><header><section id="logo"><span class="express">express<em>3.0.0</em></span><span class="description">
|
||||||
|
web application framework for <a href="http://nodejs.org">node </a></span></section><nav class="clearfix"><a href="/" class=""> Home</a><a href="/api.html" class=""> API Reference</a><a href="/guide.html" class=""> Guide</a><a href="/applications.html" class=""> Applications</a><a href="/community.html" class="active"> Community</a><a href="/faq.html" class=""> FAQ</a></nav><div id="x"></div><div id="y"></div></header><div id="boxes" class="clearfix"><section id="mailing-list"><h3>Mailing List</h3><p>Join over 1500 Express users or browse over 5000
|
||||||
|
discussions in the <a href="https://groups.google.com/group/express-js">Google Group</a>.
|
||||||
|
</p></section><section id="irc"><h3>IRC Channel</h3><p>Hundreds of developers idle in #express on freenode every day,
|
||||||
|
if you have questions about the framework jump in for quick
|
||||||
|
feedback.
|
||||||
|
</p></section><section id="examples"><h3>Examples</h3><p>View dozens of Express application <a href="https://github.com/visionmedia/express/tree/master/examples">examples</a>
|
||||||
|
in the github repository covering everything from API design and authentication
|
||||||
|
to template engine integration.
|
||||||
|
</p></section><section id="issues"><h3>Issues</h3><p>If you've come across what you think is a bug, or just want to make
|
||||||
|
a feature request open a ticket in the <a href="https://github.com/visionmedia/express/issues">issue queue</a>.
|
||||||
|
</p></section><section id="extending"><h3>Third Party</h3><p>Our vibrant community has created a large variety of extensions,
|
||||||
|
<a href="https://github.com/senchalabs/connect/wiki">middleware</a>
|
||||||
|
and higher level frameworks. Check them out in the
|
||||||
|
<a href="https://github.com/visionmedia/express/wiki">wiki</a>.</p></section></div></section><a id="top" href="#"><img src="images/arrow.png"></a><footer><div id="footer-content">© 2012 TJ Holowaychuk. All rights reserved.</div></footer></body></html>
|
||||||
14
community.jade
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
!!! 5
|
||||||
|
html
|
||||||
|
head
|
||||||
|
title Express - community
|
||||||
|
include includes/head
|
||||||
|
body.community.inner
|
||||||
|
.bar
|
||||||
|
section#content
|
||||||
|
header
|
||||||
|
include includes/logo
|
||||||
|
active = '/community.html'
|
||||||
|
include includes/menu
|
||||||
|
include en/community
|
||||||
|
include includes/footer
|
||||||
59
en/api/app-VERB.jade
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
section
|
||||||
|
h3(id='app.VERB') app.VERB(path, [callback...], callback)
|
||||||
|
|
||||||
|
p.
|
||||||
|
The <code>app.VERB()</code> methods provide the routing functionality
|
||||||
|
in Express, where <strong>VERB</strong> is one of the HTTP verbs, such
|
||||||
|
as <code>app.post()</code>. Multiple callbacks may be give, all are treated
|
||||||
|
equally, and behave just like middleware, with the one exception that
|
||||||
|
these callbacks may invoke <code>next('route')</code> to bypass the
|
||||||
|
remaining route callback(s). This mechanism can be used to perform pre-conditions
|
||||||
|
on a route then pass control to subsequent routes when there is no reason to proceed
|
||||||
|
with the route matched.
|
||||||
|
|
||||||
|
p.
|
||||||
|
The following snippet illustrates the most simple route definition possible. Express
|
||||||
|
translates the path strings to regular expressions, used internally to match incoming requests.
|
||||||
|
Query strings are <em>not</em> considered when peforming these matches, for example "GET /"
|
||||||
|
would match the following route, as would "GET /?name=tobi".
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.get('/', function(req, res){
|
||||||
|
res.send('hello world');
|
||||||
|
});
|
||||||
|
|
||||||
|
p.
|
||||||
|
Regular expressions may also be used, and can be useful
|
||||||
|
if you have very specific restraints, for example the following
|
||||||
|
would match "GET /commits/71dbb9c" as well as "GET /commits/71dbb9c..4c084f9".
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.get(/^\/commits\/(\d+)(?:\.\.(\d+))?$/, function(req, res){
|
||||||
|
var from = req.params[0];
|
||||||
|
var to = req.params[1] || 'HEAD';
|
||||||
|
res.send('commit range ' + from + '..' + to);
|
||||||
|
});
|
||||||
|
|
||||||
|
p.
|
||||||
|
Several callbacks may also be passed, useful for re-using middleware
|
||||||
|
that load resources, perform validations, etc.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.get('/user/:id', user.load, function(){
|
||||||
|
// ...
|
||||||
|
})
|
||||||
|
|
||||||
|
p.
|
||||||
|
These callbacks may be passed within arrays as well, these arrays are
|
||||||
|
simply flattened when passed:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
var middleware = [loadForum, loadThread];
|
||||||
|
|
||||||
|
app.get('/forum/:fid/thread/:tid', middleware, function(){
|
||||||
|
// ...
|
||||||
|
})
|
||||||
|
|
||||||
|
app.post('/forum/:fid/thread/:tid', middleware, function(){
|
||||||
|
// ...
|
||||||
|
})
|
||||||
34
en/api/app-all.jade
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
section
|
||||||
|
h3(id='app.all') app.all(path, [callback...], callback)
|
||||||
|
|
||||||
|
p.
|
||||||
|
This method functions just like the <code>app.VERB()</code> methods,
|
||||||
|
however it matches all HTTP verbs.
|
||||||
|
|
||||||
|
p.
|
||||||
|
This method is extremely useful for
|
||||||
|
mapping "global" logic for specific path prefixes or arbitrary matches.
|
||||||
|
For example if you placed the following route at the top of all other
|
||||||
|
route definitions, it would require that all routes from that point on
|
||||||
|
would require authentication, and automatically load a user. Keep in mind
|
||||||
|
that these callbacks do not have to act as end points, <code>loadUser</code>
|
||||||
|
can perform a task, then <code>next()</code> to continue matching subsequent
|
||||||
|
routes.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.all('*', requireAuthentication, loadUser);
|
||||||
|
|
||||||
|
p.
|
||||||
|
Or the equivalent:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.all('*', requireAuthentication)
|
||||||
|
app.all('*', loadUser);
|
||||||
|
|
||||||
|
p.
|
||||||
|
Another great example of this is white-listed "global" functionality. Here
|
||||||
|
the example is much like before, however only restricting paths prefixed with
|
||||||
|
"/api":
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.all('/api/*', requireAuthentication);
|
||||||
40
en/api/app-configure.jade
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
section
|
||||||
|
h3(id='app.configure') app.configure([env], callback)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Conditionally invoke <code>callback</code> when <code>env</code> matches <code>app.get('env')</code>,
|
||||||
|
aka <code>process.env.NODE_ENV</code>. This method remains for legacy reason, and is effectively
|
||||||
|
an <code>if</code> statement as illustrated in the following snippets. These functions are <em>not</em>
|
||||||
|
required in order to use <code>app.set()</code> and other configuration methods.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
// all environments
|
||||||
|
app.configure(function(){
|
||||||
|
app.set('title', 'My Application');
|
||||||
|
})
|
||||||
|
|
||||||
|
// development only
|
||||||
|
app.configure('development', function(){
|
||||||
|
app.set('db uri', 'localhost/dev');
|
||||||
|
})
|
||||||
|
|
||||||
|
// production only
|
||||||
|
app.configure('production', function(){
|
||||||
|
app.set('db uri', 'n.n.n.n/prod');
|
||||||
|
})
|
||||||
|
|
||||||
|
Is effectively sugar for:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
// all environments
|
||||||
|
app.set('title', 'My Application');
|
||||||
|
|
||||||
|
// development only
|
||||||
|
if ('development' == app.get('env')) {
|
||||||
|
app.set('db uri', 'localhost/dev');
|
||||||
|
}
|
||||||
|
|
||||||
|
// production only
|
||||||
|
if ('production' == app.get('env')) {
|
||||||
|
app.set('db uri', 'n.n.n.n/prod');
|
||||||
|
}
|
||||||
10
en/api/app-disable.jade
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
section
|
||||||
|
h3(id='app.disable') app.disable(name)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Set setting <code>name</code> to <code>false</code>.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.disable('trust proxy');
|
||||||
|
app.get('trust proxy');
|
||||||
|
// => false
|
||||||
13
en/api/app-disabled.jade
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
section
|
||||||
|
h3(id='app.disabled') app.disabled(name)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Check if setting <code>name</code> is disabled.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.disabled('trust proxy');
|
||||||
|
// => true
|
||||||
|
|
||||||
|
app.enable('trust proxy');
|
||||||
|
app.disabled('trust proxy');
|
||||||
|
// => false
|
||||||
10
en/api/app-enable.jade
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
section
|
||||||
|
h3(id='app.enable') app.enable(name)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Set setting <code>name</code> to <code>true</code>.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.enable('trust proxy');
|
||||||
|
app.get('trust proxy');
|
||||||
|
// => true
|
||||||
13
en/api/app-enabled.jade
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
section
|
||||||
|
h3(id='app.enabled') app.enabled(name)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Check if setting <code>name</code> is enabled.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.enabled('trust proxy');
|
||||||
|
// => false
|
||||||
|
|
||||||
|
app.enable('trust proxy');
|
||||||
|
app.enabled('trust proxy');
|
||||||
|
// => true
|
||||||
41
en/api/app-engine.jade
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
section
|
||||||
|
h3(id='app.engine') app.engine(ext, callback)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Register the given template engine <code>callback</code> as <code>ext</code>
|
||||||
|
|
||||||
|
By default will <code>require()</code> the engine based on the
|
||||||
|
file extension. For example if you try to render
|
||||||
|
a "foo.jade" file Express will invoke the following internally,
|
||||||
|
and cache the <code>require()</code> on subsequent calls to increase
|
||||||
|
performance.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.engine('jade', require('jade').__express);
|
||||||
|
|
||||||
|
p.
|
||||||
|
For engines that do not provide <code>.__express</code> out of the box -
|
||||||
|
or if you wish to "map" a different extension to the template engine
|
||||||
|
you may use this method. For example mapping the EJS template engine to
|
||||||
|
".html" files:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.engine('html', require('ejs').renderFile);
|
||||||
|
|
||||||
|
p.
|
||||||
|
In this case EJS provides a <code>.renderFile()</code> method with
|
||||||
|
the same signature that Express expects: <code>(path, options, callback)</code>,
|
||||||
|
though note that it aliases this method as <code>ejs.__express</code> internally
|
||||||
|
so if you're using ".ejs" extensions you dont need to do anything.
|
||||||
|
|
||||||
|
p.
|
||||||
|
Some template engines do not follow this convention, the
|
||||||
|
<a href="https://github.com/visionmedia/consolidate.js">consolidate.js</a>
|
||||||
|
library was created to map all of node's popular template
|
||||||
|
engines to follow this convention, thus allowing them to
|
||||||
|
work seemlessly within Express.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
var engines = require('consolidate');
|
||||||
|
app.engine('haml', engines.haml);
|
||||||
|
app.engine('html', engines.hogan);
|
||||||
13
en/api/app-get.jade
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
section
|
||||||
|
h3(id='app.get') app.get(name)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Get setting <code>name</code> value.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.get('title');
|
||||||
|
// => undefined
|
||||||
|
|
||||||
|
app.set('title', 'My Site');
|
||||||
|
app.get('title');
|
||||||
|
// => "My Site"
|
||||||
37
en/api/app-listen.jade
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
section
|
||||||
|
h3(id='app.listen') app.listen()
|
||||||
|
|
||||||
|
p.
|
||||||
|
Bind and listen for connections on the given host and port,
|
||||||
|
this method is identical to node's <a href="http://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback">http.Server#listen()</a>.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
var express = require('express');
|
||||||
|
var app = express();
|
||||||
|
app.listen(3000);
|
||||||
|
|
||||||
|
p.
|
||||||
|
The <code>app</code> returned by <code>express()</code> is in fact a JavaScript
|
||||||
|
<code>Function</code>, designed to be passed to node's http servers as a callback
|
||||||
|
to handle requests. This allows you to provide both HTTP and HTTPS versions of
|
||||||
|
your app with the same codebase easily, as the app does not inherit from these,
|
||||||
|
it is simply a callback:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
var express = require('express');
|
||||||
|
var https = require('https');
|
||||||
|
var http = require('http');
|
||||||
|
var app = express();
|
||||||
|
|
||||||
|
http.createServer(app).listen(80);
|
||||||
|
https.createServer(options, app).listen(443);
|
||||||
|
|
||||||
|
p.
|
||||||
|
The <code>app.listen()</code> method is simply a convenience method defined as,
|
||||||
|
if you wish to use HTTPS or provide both, use the technique above.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.listen = function(){
|
||||||
|
var server = http.createServer(this);
|
||||||
|
return server.listen.apply(server, arguments);
|
||||||
|
};
|
||||||
37
en/api/app-locals.jade
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
section
|
||||||
|
h3(id='app.locals') app.locals
|
||||||
|
|
||||||
|
p.
|
||||||
|
Application local variables are provided to all templates
|
||||||
|
rendered within the application. This is useful for providing
|
||||||
|
helper functions to templates, as well as app-level data.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.locals.title = 'My App';
|
||||||
|
app.locals.strftime = require('strftime');
|
||||||
|
|
||||||
|
p.
|
||||||
|
The <code>app.locals</code> object is a JavaScript <code>Function</code>,
|
||||||
|
which when invoked with an object will merge properties into itself, providing
|
||||||
|
a simple way to expose existing objects as local variables.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.locals({
|
||||||
|
title: 'My App',
|
||||||
|
phone: '1-250-858-9990',
|
||||||
|
email: 'me@myapp.com'
|
||||||
|
});
|
||||||
|
|
||||||
|
app.locals.title
|
||||||
|
// => 'My App'
|
||||||
|
|
||||||
|
app.locals.email
|
||||||
|
// => 'me@myapp.com'
|
||||||
|
|
||||||
|
p.
|
||||||
|
By default Express exposes only a single app-level local variable, code>settings</code>.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.set('title', 'My App');
|
||||||
|
// use settings.title in a view
|
||||||
|
|
||||||
73
en/api/app-param.jade
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
section
|
||||||
|
h3(id='app.param') app.param([name], callback)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Map logic to route parameters. For example when <code>:user</code>
|
||||||
|
is present in a route path you may map user loading logic to automatically
|
||||||
|
provide <code>req.user</code> to the route, or perform validations
|
||||||
|
on the parameter input.
|
||||||
|
|
||||||
|
p.
|
||||||
|
The following snippet illustrates how the <code>callback</code>
|
||||||
|
is much like middleware, thus supporting async operations, however
|
||||||
|
providing the additional value of the parameter, here named as <code>id</code>.
|
||||||
|
An attempt to load the user is then performed, assigning <code>req.user</code>,
|
||||||
|
otherwise passing an error to <code>next(err)</code>.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.param('user', function(req, res, next, id){
|
||||||
|
User.find(id, function(err, user){
|
||||||
|
if (err) {
|
||||||
|
next(err);
|
||||||
|
} else if (user) {
|
||||||
|
req.user = user;
|
||||||
|
next();
|
||||||
|
} else {
|
||||||
|
next(new Error('failed to load user'));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
p.
|
||||||
|
Alternatively you may pass only a <code>callback</code>, in which
|
||||||
|
case you have the opportunity to alter the <code>app.param()</code> API.
|
||||||
|
For example the <a href="http://github.com/visionmedia/express-params">express-params</a>
|
||||||
|
defines the following callback which allows you to restrict parameters to a given
|
||||||
|
regular expression.
|
||||||
|
|
||||||
|
p.
|
||||||
|
This example is a bit more advanced, checking if the second argument is a regular
|
||||||
|
expression, returning the callback which acts much like the "user" param example.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.param(function(name, fn){
|
||||||
|
if (fn instanceof RegExp) {
|
||||||
|
return function(req, res, next, val){
|
||||||
|
var captures;
|
||||||
|
if (captures = fn.exec(String(val))) {
|
||||||
|
req.params[name] = captures;
|
||||||
|
next();
|
||||||
|
} else {
|
||||||
|
next('route');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
p.
|
||||||
|
The method could now be used to effectively validate parameters, or also
|
||||||
|
parse them to provide capture groups:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.param('id', /^\d+$/);
|
||||||
|
|
||||||
|
app.get('/user/:id', function(req, res){
|
||||||
|
res.send('user ' + req.params.id);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.param('range', /^(\w+)\.\.(\w+)?$/);
|
||||||
|
|
||||||
|
app.get('/range/:range', function(req, res){
|
||||||
|
var range = req.params.range;
|
||||||
|
res.send('from ' + range[1] + ' to ' + range[2]);
|
||||||
|
});
|
||||||
16
en/api/app-render.jade
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
section
|
||||||
|
h3(id='app.render') app.render(view, [options], callback)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Render a <code>view</code> with a callback responding with
|
||||||
|
the rendered string. This is the app-level variant of <code>res.render()</code>,
|
||||||
|
and otherwise behaves the same way.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.render('email', function(err, html){
|
||||||
|
// ...
|
||||||
|
});
|
||||||
|
|
||||||
|
app.render('email', { name: 'Tobi' }, function(err, html){
|
||||||
|
// ...
|
||||||
|
});
|
||||||
31
en/api/app-routes.jade
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
|
||||||
|
section
|
||||||
|
h3(id='app.routes') app.routes
|
||||||
|
|
||||||
|
p.
|
||||||
|
The <code>app.routes</code> object houses all of the routes defined mapped
|
||||||
|
by the associated HTTP verb. This object may be used for introspection capabilities,
|
||||||
|
for example Express uses this internally not only for routing but to provide default
|
||||||
|
<string>OPTIONS</string> behaviour unless <code>app.options()</code> is used. Your application
|
||||||
|
or framework may also remove routes by simply by removing them from this object.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
console.log(app.routes)
|
||||||
|
|
||||||
|
{ get:
|
||||||
|
[ { path: '/',
|
||||||
|
method: 'get',
|
||||||
|
callbacks: [Object],
|
||||||
|
keys: [],
|
||||||
|
regexp: /^\/\/?$/i },
|
||||||
|
{ path: '/user/:id',
|
||||||
|
method: 'get',
|
||||||
|
callbacks: [Object],
|
||||||
|
keys: [{ name: 'id', optional: false }],
|
||||||
|
regexp: /^\/user\/(?:([^\/]+?))\/?$/i } ],
|
||||||
|
delete:
|
||||||
|
[ { path: '/user/:id',
|
||||||
|
method: 'delete',
|
||||||
|
callbacks: [Object],
|
||||||
|
keys: [Object],
|
||||||
|
regexp: /^\/user\/(?:([^\/]+?))\/?$/i } ] }
|
||||||
10
en/api/app-set.jade
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
section
|
||||||
|
h3(id='app.set') app.set(name, value)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Assigns setting <code>name</code> to <code>value</code>.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.set('title', 'My Site');
|
||||||
|
app.get('title');
|
||||||
|
// => "My Site"
|
||||||
41
en/api/app-settings.jade
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
section
|
||||||
|
h3(id='app-settings') settings
|
||||||
|
|
||||||
|
p.
|
||||||
|
The following settings are provided to alter how Express will behave:
|
||||||
|
|
||||||
|
ul
|
||||||
|
li
|
||||||
|
code env
|
||||||
|
| Environment mode, defaults to <code>process.env.NODE_ENV</code> or "development"
|
||||||
|
li
|
||||||
|
code trust proxy
|
||||||
|
| Enables reverse proxy support, disabled by default
|
||||||
|
li
|
||||||
|
code jsonp callback
|
||||||
|
| Enables jsonp callback support, enabled by default
|
||||||
|
li
|
||||||
|
code jsonp callback name
|
||||||
|
| Changes the default callback name of <code>?callback=</code>
|
||||||
|
li
|
||||||
|
code json replacer
|
||||||
|
| JSON replacer callback, null by default
|
||||||
|
li
|
||||||
|
code json spaces
|
||||||
|
| JSON response spaces for formatting, defaults to <code>2</code> in development, <code>0</code> in production
|
||||||
|
li
|
||||||
|
code case sensitive routing
|
||||||
|
| Enable case sensitivity, disabled by default, treating "/Foo" and "/foo" as the same
|
||||||
|
li
|
||||||
|
code strict routing
|
||||||
|
| Enable strict routing, by default "/foo" and "/foo/" are treated the same by the router
|
||||||
|
li
|
||||||
|
code view cache
|
||||||
|
| Enables view template compilation caching, enabled in production by default
|
||||||
|
li
|
||||||
|
code view engine
|
||||||
|
| The default engine extension to use when omitted
|
||||||
|
li
|
||||||
|
code views
|
||||||
|
| The view directory path
|
||||||
|
|
||||||
16
en/api/app.jade
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
include ./app-set
|
||||||
|
include ./app-get
|
||||||
|
include ./app-enable
|
||||||
|
include ./app-disable
|
||||||
|
include ./app-enabled
|
||||||
|
include ./app-disabled
|
||||||
|
include ./app-configure
|
||||||
|
include ./app-settings
|
||||||
|
include ./app-engine
|
||||||
|
include ./app-param
|
||||||
|
include ./app-VERB
|
||||||
|
include ./app-all
|
||||||
|
include ./app-locals
|
||||||
|
include ./app-render
|
||||||
|
include ./app-routes
|
||||||
|
include ./app-listen
|
||||||
15
en/api/express.jade
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
section
|
||||||
|
h3(id='express') express()
|
||||||
|
|
||||||
|
p.
|
||||||
|
Create an express application.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
var express = require('express');
|
||||||
|
var app = express();
|
||||||
|
|
||||||
|
app.get('/', function(req, res){
|
||||||
|
res.send('hello world');
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(3000);
|
||||||
68
en/api/menu.jade
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
ul#menu
|
||||||
|
li#app-api
|
||||||
|
a(href='#express') Application
|
||||||
|
ul#app-menu
|
||||||
|
li: a(href='#express') express()
|
||||||
|
li: a(href='#app.set') app.set()
|
||||||
|
li: a(href='#app.get') app.get()
|
||||||
|
li: a(href='#app.enable') app.enable()
|
||||||
|
li: a(href='#app.disable') app.disable()
|
||||||
|
li: a(href='#app.enabled') app.endabled()
|
||||||
|
li: a(href='#app.disabled') app.disabled()
|
||||||
|
li: a(href='#app.configure') app.configure()
|
||||||
|
li: a(href='#app.engine') app.engine()
|
||||||
|
li: a(href='#app.param') app.param()
|
||||||
|
li: a(href='#app-settings') application settings
|
||||||
|
li: a(href='#app.VERB') application routing
|
||||||
|
li: a(href='#app.all') app.all()
|
||||||
|
li: a(href='#app.locals') app.locals
|
||||||
|
li: a(href='#app.render') app.render()
|
||||||
|
li: a(href='#app.routes') app.routes
|
||||||
|
li: a(href='#app.listen') app.listen()
|
||||||
|
li#req-api
|
||||||
|
a(href='#req.params') Request
|
||||||
|
ul#req-menu
|
||||||
|
li: a(href='#req.params') req.params
|
||||||
|
li: a(href='#req.query') req.query
|
||||||
|
li: a(href='#req.body') req.body
|
||||||
|
li: a(href='#req.param') req.param()
|
||||||
|
li: a(href='#req.cookies') req.cookies
|
||||||
|
li: a(href='#req.signedCookies') req.signedCookies
|
||||||
|
li: a(href='#req.get') req.get()
|
||||||
|
li: a(href='#req.accepts') req.accepts()
|
||||||
|
li: a(href='#req.accepted') req.accepted
|
||||||
|
li: a(href='#req.is') req.is()
|
||||||
|
li: a(href='#req.ip') req.ip
|
||||||
|
li: a(href='#req.ips') req.ips
|
||||||
|
li: a(href='#req.path') req.path
|
||||||
|
li: a(href='#req.host') req.host
|
||||||
|
li: a(href='#req.fresh') req.fresh
|
||||||
|
li: a(href='#req.stale') req.stale
|
||||||
|
li: a(href='#req.xhr') req.xhr
|
||||||
|
li: a(href='#req.protocol') req.protocol
|
||||||
|
li: a(href='#req.secure') req.secure
|
||||||
|
li: a(href='#req.subdomains') req.subdomains
|
||||||
|
li: a(href='#req.acceptedLanguages') req.acceptedLanguages
|
||||||
|
li: a(href='#req.acceptedCharsets') req.acceptedCharsets
|
||||||
|
li: a(href='#req.acceptsCharset') req.acceptsCharset()
|
||||||
|
li: a(href='#req.acceptsLanguage') req.acceptsLanguage()
|
||||||
|
li#res-api
|
||||||
|
a(href='#res.status') Response
|
||||||
|
ul#res-menu
|
||||||
|
li: a(href='#res.status') res.status()
|
||||||
|
li: a(href='#res.set') res.set()
|
||||||
|
li: a(href='#res.get') res.get()
|
||||||
|
li: a(href='#res.cookie') res.cookie()
|
||||||
|
li: a(href='#res.clearCookie') res.clearCookie()
|
||||||
|
li: a(href='#res.redirect') res.redirect()
|
||||||
|
li: a(href='#res.charset') res.charset
|
||||||
|
li: a(href='#res.send') res.send()
|
||||||
|
li: a(href='#res.json') res.json()
|
||||||
|
li: a(href='#res.type') res.type()
|
||||||
|
li: a(href='#res.format') res.format()
|
||||||
|
li: a(href='#res.attachment') res.attachment()
|
||||||
|
li: a(href='#res.sendfile') res.sendfile()
|
||||||
|
li: a(href='#res.download') res.download()
|
||||||
|
li: a(href='#res.links') res.links()
|
||||||
|
li: a(href='#res.locals') res.locals
|
||||||
|
li: a(href='#res.render') res.render()
|
||||||
15
en/api/req-accepted.jade
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.accepted') req.accepted
|
||||||
|
|
||||||
|
p.
|
||||||
|
Return an array of Accepted media types ordered from highest quality to lowest.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
[ { value: 'application/json',
|
||||||
|
quality: 1,
|
||||||
|
type: 'application',
|
||||||
|
subtype: 'json' },
|
||||||
|
{ value: 'text/html',
|
||||||
|
quality: 0.5,
|
||||||
|
type: 'text',
|
||||||
|
subtype: 'html' } ]
|
||||||
9
en/api/req-acceptedCharsets.jade
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.acceptedCharsets') req.acceptedCharsets
|
||||||
|
|
||||||
|
p.
|
||||||
|
Return an array of Accepted charsets ordered from highest quality to lowest.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8
|
||||||
|
// => ['unicode-1-1', 'iso-8859-5']
|
||||||
9
en/api/req-acceptedLanguages.jade
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.acceptedLanguages') req.acceptedLanguages
|
||||||
|
|
||||||
|
p.
|
||||||
|
Return an array of Accepted languages ordered from highest quality to lowest.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
Accept-Language: en;q=.5, en-us
|
||||||
|
// => ['en-us', 'en']
|
||||||
38
en/api/req-accepts.jade
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.accepts') req.accepts(types)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Check if the given <code>types</code> are acceptable, returning
|
||||||
|
the best match when true, otherwise <code>undefined</code> - in which
|
||||||
|
case you should respond with 406 "Not Acceptable".
|
||||||
|
|
||||||
|
p.
|
||||||
|
The <code>type</code> value may be a single mime type string
|
||||||
|
such as "application/json", the extension name
|
||||||
|
such as "json", a comma-delimted list or an array. When a list
|
||||||
|
or array is given the <em>best</em> match, if any is returned.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
// Accept: text/html
|
||||||
|
req.accepts('html');
|
||||||
|
// => "html"
|
||||||
|
|
||||||
|
// Accept: text/*, application/json
|
||||||
|
req.accepts('html');
|
||||||
|
// => "html"
|
||||||
|
req.accepts('text/html');
|
||||||
|
// => "text/html"
|
||||||
|
req.accepts('json, text');
|
||||||
|
// => "json"
|
||||||
|
req.accepts('application/json');
|
||||||
|
// => "application/json"
|
||||||
|
|
||||||
|
// Accept: text/*, application/json
|
||||||
|
req.accepts('image/png');
|
||||||
|
req.accepts('png');
|
||||||
|
// => undefined
|
||||||
|
|
||||||
|
// Accept: text/*;q=.5, application/json
|
||||||
|
req.accepts(['html', 'json']);
|
||||||
|
req.accepts('html, json');
|
||||||
|
// => "json"
|
||||||
5
en/api/req-acceptsCharset.jade
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.acceptsCharset') req.acceptsCharset(charset)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Check if the given <code>charset</code> are acceptable.
|
||||||
5
en/api/req-acceptsLanguage.jade
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.acceptsLanguage') req.acceptsLanguage(lang)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Check if the given <code>lang</code> are acceptable.
|
||||||
20
en/api/req-body.jade
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.body') req.body
|
||||||
|
|
||||||
|
p.
|
||||||
|
This property is an object containing the parsed request body. This feature
|
||||||
|
is provided by the <code>bodyParser()</code> middleware, though other body
|
||||||
|
parsing middleware may follow this convention as well. This property
|
||||||
|
defaults to <code>{}</code> when <code>bodyParser()</code> is used.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
// POST user[name]=tobi&user[email]=tobi@learnboost.com
|
||||||
|
req.body.user.name
|
||||||
|
// => "tobi"
|
||||||
|
|
||||||
|
req.body.user.email
|
||||||
|
// => "tobi@learnboost.com"
|
||||||
|
|
||||||
|
// POST { "name": "tobi" }
|
||||||
|
req.body.name
|
||||||
|
// => "tobi"
|
||||||
12
en/api/req-cookies.jade
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.cookies') req.cookies
|
||||||
|
|
||||||
|
p.
|
||||||
|
When the <code>cookieParser()</code> middleware is used this object
|
||||||
|
defaults to <code>{}</code>, otherwise contains the cookies sent by
|
||||||
|
the user-agent.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
// Cookie: name=tj
|
||||||
|
req.cookies.name
|
||||||
|
// => "tj"
|
||||||
10
en/api/req-fresh.jade
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.fresh') req.fresh
|
||||||
|
|
||||||
|
p.
|
||||||
|
Check if the request is fresh - aka Last-Modified and/or the ETag still match,
|
||||||
|
indicating that the resource is "fresh".
|
||||||
|
|
||||||
|
+js.
|
||||||
|
req.fresh
|
||||||
|
// => true
|
||||||
17
en/api/req-header.jade
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.get') req.get(field)
|
||||||
|
p
|
||||||
|
| Get the case-insensitive request header <code>field</code>.
|
||||||
|
| The <em>Referrer</em> and <em>Referer</em> fields are interchangeable.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
req.get('Content-Type');
|
||||||
|
// => "text/plain"
|
||||||
|
|
||||||
|
req.get('content-type');
|
||||||
|
// => "text/plain"
|
||||||
|
|
||||||
|
req.get('Something');
|
||||||
|
// => undefined
|
||||||
|
|
||||||
|
p Aliased as <code>req.header(field)</code>.
|
||||||
10
en/api/req-host.jade
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.host') req.host
|
||||||
|
|
||||||
|
p.
|
||||||
|
Returns the hostname from the "Host" header field (void of portno).
|
||||||
|
|
||||||
|
+js.
|
||||||
|
// Host: "example.com:3000"
|
||||||
|
req.host
|
||||||
|
// => "example.com"
|
||||||
10
en/api/req-ip.jade
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.ip') req.ip
|
||||||
|
|
||||||
|
p.
|
||||||
|
Return the remote address, or when "trust proxy"
|
||||||
|
is enabled - the upstream address.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
req.ip
|
||||||
|
// => "127.0.0.1"
|
||||||
12
en/api/req-ips.jade
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.ips') req.ips
|
||||||
|
|
||||||
|
p.
|
||||||
|
When "trust proxy" is `true`, parse
|
||||||
|
the "X-Forwarded-For" ip address list
|
||||||
|
and return an array, otherwise an empty
|
||||||
|
array is returned.
|
||||||
|
|
||||||
|
For example if the value were "client, proxy1, proxy2"
|
||||||
|
you would receive the array <code>["client", "proxy1", "proxy2"]</code>
|
||||||
|
where "proxy2" is the furthest down-stream.
|
||||||
22
en/api/req-is.jade
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.is') req.is(type)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Check if the incoming request contains the "Content-Type"
|
||||||
|
header field, and it matches the give mime <code>type</code>.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
// With Content-Type: text/html; charset=utf-8
|
||||||
|
req.is('html');
|
||||||
|
req.is('text/html');
|
||||||
|
req.is('text/*');
|
||||||
|
// => true
|
||||||
|
|
||||||
|
// When Content-Type is application/json
|
||||||
|
req.is('json');
|
||||||
|
req.is('application/json');
|
||||||
|
req.is('application/*');
|
||||||
|
// => true
|
||||||
|
|
||||||
|
req.is('html');
|
||||||
|
// => false
|
||||||
30
en/api/req-param.jade
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.param') req.param(name)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Return the value of param <code>name</code> when present.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
// ?name=tobi
|
||||||
|
req.param('name')
|
||||||
|
// => "tobi"
|
||||||
|
|
||||||
|
// POST name=tobi
|
||||||
|
req.param('name')
|
||||||
|
// => "tobi"
|
||||||
|
|
||||||
|
// /user/tobi for /user/:name
|
||||||
|
req.param('name')
|
||||||
|
// => "tobi"
|
||||||
|
|
||||||
|
p Lookup is performed in the following order:
|
||||||
|
|
||||||
|
ul
|
||||||
|
li <code>req.params</code>
|
||||||
|
li <code>req.body</code>
|
||||||
|
li <code>req.query</code>
|
||||||
|
|
||||||
|
p.
|
||||||
|
Direct access to <code>req.body</code>, <code>req.params</code>,
|
||||||
|
and <code>req.query</code> should be favoured for clarity - unless
|
||||||
|
you truly accept input from each object.
|
||||||
23
en/api/req-params.jade
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.params') req.params
|
||||||
|
|
||||||
|
p.
|
||||||
|
This property is an array containing properties mapped to the named route "parameters".
|
||||||
|
For example if you have the route <code>/user/:name</code>, then the "name" property
|
||||||
|
is available to you as <code>req.params.name</code>. This object defaults to <code>{}</code>.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
// GET /user/tj
|
||||||
|
req.params.name
|
||||||
|
// => "tj"
|
||||||
|
|
||||||
|
p.
|
||||||
|
When a regular expression is used for the route definition, capture groups
|
||||||
|
are provided in the array using <code>req.params[N]</code>, where <code>N</code>
|
||||||
|
is the nth capture group. This rule is applied to unnamed wild-card matches
|
||||||
|
with string routes such as `/file/*`:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
// GET /file/javascripts/jquery.js
|
||||||
|
req.params[0]
|
||||||
|
// => "javascripts/jquery.js"
|
||||||
10
en/api/req-path.jade
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.path') req.path
|
||||||
|
|
||||||
|
p.
|
||||||
|
Returns the request URL pathname.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
// example.com/users?sort=desc
|
||||||
|
req.path
|
||||||
|
// => "/users"
|
||||||
14
en/api/req-protocol.jade
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.protocol') req.protocol
|
||||||
|
|
||||||
|
p.
|
||||||
|
Return the protocol string "http" or "https"
|
||||||
|
when requested with TLS. When the "trust proxy"
|
||||||
|
setting is enabled the "X-Forwarded-Proto" header
|
||||||
|
field will be trusted. If you're running behind
|
||||||
|
a reverse proxy that supplies https for you this
|
||||||
|
may be enabled.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
req.protocol
|
||||||
|
// => "http"
|
||||||
21
en/api/req-query.jade
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.query') req.query
|
||||||
|
|
||||||
|
p.
|
||||||
|
This property is an object containing the parsed query-string,
|
||||||
|
defaulting to <code>{}</code>.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
// GET /search?q=tobi+ferret
|
||||||
|
req.query.q
|
||||||
|
// => "tobi ferret"
|
||||||
|
|
||||||
|
// GET /shoes?order=desc&shoe[color]=blue&shoe[type]=converse
|
||||||
|
req.query.order
|
||||||
|
// => "desc"
|
||||||
|
|
||||||
|
req.query.show.color
|
||||||
|
// => "blue"
|
||||||
|
|
||||||
|
req.query.show.converse
|
||||||
|
// => "converse"
|
||||||
8
en/api/req-secure.jade
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.secure') req.secure
|
||||||
|
|
||||||
|
p.
|
||||||
|
Check if a TLS connection is established. This is a short-hand for:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
return 'https' == this.protocol;
|
||||||
16
en/api/req-signedCookies.jade
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.signedCookies') req.signedCookies
|
||||||
|
|
||||||
|
p.
|
||||||
|
When the <code>cookieParser(secret)</code> middleware is used this object
|
||||||
|
defaults to <code>{}</code>, otherwise contains the signed cookies sent by
|
||||||
|
the user-agent, unsigned and ready for use. Signed cookies reside in a different
|
||||||
|
object to show developer intent, otherwise a malicious attack could be
|
||||||
|
placed on `req.cookie` values which are easy to spoof. Note that signing
|
||||||
|
a cookie does not mean it is "hidden" nor encrypted, this simply prevents
|
||||||
|
tampering as the secret used to sign is private.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
// Cookie: user=tobi.CP7AWaXDfAKIRfH49dQzKJx7sKzzSoPq7/AcBBRVwlI3
|
||||||
|
req.signedCookies.user
|
||||||
|
// => "tobi"
|
||||||
10
en/api/req-stale.jade
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.stale') req.stale
|
||||||
|
|
||||||
|
p.
|
||||||
|
Check if the request is stale - aka Last-Modified and/or the ETag do not match,
|
||||||
|
indicating that the resource is "stale".
|
||||||
|
|
||||||
|
+js.
|
||||||
|
req.stale
|
||||||
|
// => true
|
||||||
10
en/api/req-subdomains.jade
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.subdomains') req.subdomains
|
||||||
|
|
||||||
|
p.
|
||||||
|
Return subdomains as an array.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
// Host: "tobi.ferrets.example.com"
|
||||||
|
req.subdomains
|
||||||
|
// => ["ferrets", "tobi"]
|
||||||
10
en/api/req-xhr.jade
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
section
|
||||||
|
h3(id='req.xhr') req.xhr
|
||||||
|
|
||||||
|
p.
|
||||||
|
Check if the request was issued with the "X-Request-With"
|
||||||
|
header field set to "XMLHttpRequest" (jQuery etc).
|
||||||
|
|
||||||
|
+js.
|
||||||
|
req.xhr
|
||||||
|
// => true
|
||||||
24
en/api/req.jade
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
include ./req-params
|
||||||
|
include ./req-query
|
||||||
|
include ./req-body
|
||||||
|
include ./req-param
|
||||||
|
include ./req-cookies
|
||||||
|
include ./req-signedCookies
|
||||||
|
include ./req-header
|
||||||
|
include ./req-accepts
|
||||||
|
include ./req-accepted
|
||||||
|
include ./req-is
|
||||||
|
include ./req-ip
|
||||||
|
include ./req-ips
|
||||||
|
include ./req-path
|
||||||
|
include ./req-host
|
||||||
|
include ./req-fresh
|
||||||
|
include ./req-stale
|
||||||
|
include ./req-xhr
|
||||||
|
include ./req-protocol
|
||||||
|
include ./req-secure
|
||||||
|
include ./req-subdomains
|
||||||
|
include ./req-acceptedLanguages
|
||||||
|
include ./req-acceptedCharsets
|
||||||
|
include ./req-acceptsCharset
|
||||||
|
include ./req-acceptsLanguage
|
||||||
16
en/api/res-attachment.jade
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.attachment') res.attachment([filename])
|
||||||
|
|
||||||
|
p.
|
||||||
|
Sets the Content-Disposition header field to "attachment". If
|
||||||
|
a <code>filename</code> is given then the Content-Type will be
|
||||||
|
automatically set based on the extname via <code>res.type()</code>,
|
||||||
|
and the Content-Disposition's "filename=" parameter will be set.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.attachment();
|
||||||
|
// Content-Disposition: attachment
|
||||||
|
|
||||||
|
res.attachment('path/to/logo.png');
|
||||||
|
// Content-Disposition: attachment; filename="logo.png"
|
||||||
|
// Content-Type: image/png
|
||||||
10
en/api/res-charset.jade
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.charset') res.charset
|
||||||
|
|
||||||
|
p.
|
||||||
|
Assign the charset. Defaults to "utf-8".
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.charset = 'value';
|
||||||
|
res.send('<p>some html</p>');
|
||||||
|
// => Content-Type: text/html; charset=value
|
||||||
9
en/api/res-clearCookie.jade
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.clearCookie') res.clearCookie(name, [options])
|
||||||
|
p.
|
||||||
|
Clear cookie <code>name</code>. The <code>path</code>
|
||||||
|
option defaults to "/".
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.cookie('name', 'tobi', { path: '/admin' });
|
||||||
|
res.clearCookie('name', { path: '/admin' });
|
||||||
26
en/api/res-cookie.jade
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.cookie') res.cookie(name, value, [options])
|
||||||
|
p.
|
||||||
|
Set cookie <code>name</code> to <code>value</code>, where
|
||||||
|
which may be a string or object converted to JSON. The <code>path</code>
|
||||||
|
option defaults to "/".
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true });
|
||||||
|
res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });
|
||||||
|
|
||||||
|
p.
|
||||||
|
The <code>maxAge</code> option is a convenience option for setting "expires"
|
||||||
|
relative to the current time in milliseconds. The following is equivalent to
|
||||||
|
the previous example.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })
|
||||||
|
|
||||||
|
p.
|
||||||
|
An object may be passed which is then serialized as JSON, which is
|
||||||
|
automatically parsed by the <code>bodyParser()</code> middleware.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.cookie('cart', { items: [1,2,3] });
|
||||||
|
res.cookie('cart', { items: [1,2,3] }, { maxAge: 900000 });
|
||||||
28
en/api/res-download.jade
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.download') res.download(path, [filename], [fn])
|
||||||
|
|
||||||
|
p.
|
||||||
|
Transfer the file at <code>path</code> as an "attachment",
|
||||||
|
typically browsers will prompt the user for download. The
|
||||||
|
Content-Disposition "filename=" parameter, aka the one
|
||||||
|
that will appear in the brower dialog is set to <code>path</code>
|
||||||
|
by default, however you may provide an override <code>filename</code>.
|
||||||
|
|
||||||
|
p.
|
||||||
|
When an error has ocurred or transfer is complete the optional
|
||||||
|
callback <code>fn</code> is invoked. This method uses <a href="#res.sendfile">res.sendfile()</a>
|
||||||
|
to transfer the file.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.download('/report-12345.pdf');
|
||||||
|
|
||||||
|
res.download('/report-12345.pdf', 'report.pdf');
|
||||||
|
|
||||||
|
res.download('/report-12345.pdf', 'report.pdf', function(err){
|
||||||
|
if (err) {
|
||||||
|
// handle error, keep in mind the response may be partially-sent
|
||||||
|
// so check res.headerSent
|
||||||
|
} else {
|
||||||
|
// decrement a download credit etc
|
||||||
|
}
|
||||||
|
});
|
||||||
56
en/api/res-format.jade
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.format') res.format(object)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Performs content-negotiation on the request Accept header
|
||||||
|
field when present. This method uses <code>req.accepted</code>, an array of
|
||||||
|
acceptable types ordered by their quality values, otherwise the
|
||||||
|
first callback is invoked. When no match is performed the server
|
||||||
|
responds with 406 "Not Acceptable", or invokes the <code>default</code>
|
||||||
|
callback.
|
||||||
|
|
||||||
|
p.
|
||||||
|
The Content-Type is set for you when a callback is selected,
|
||||||
|
however you may alter this within the callback using <code>res.set()</code>
|
||||||
|
or <code>res.type()</code> etcetera.
|
||||||
|
|
||||||
|
p.
|
||||||
|
The following example would respond with <code>{ "message": "hey" }</code>
|
||||||
|
when the Accept header field is set to "application/json" or "*/json",
|
||||||
|
however if "*/*" is given then "hey" will be the response.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.format({
|
||||||
|
'text/plain': function(){
|
||||||
|
res.send('hey');
|
||||||
|
},
|
||||||
|
|
||||||
|
'text/html': function(){
|
||||||
|
res.send('<p>hey</p>');
|
||||||
|
},
|
||||||
|
|
||||||
|
'appliation/json': function(){
|
||||||
|
res.send({ message: 'hey' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
p.
|
||||||
|
In addition to canonicalized MIME types you may also
|
||||||
|
use extnames mapped to these types, providing a slightly
|
||||||
|
less verbose implementation:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.format({
|
||||||
|
text: function(){
|
||||||
|
res.send('hey');
|
||||||
|
},
|
||||||
|
|
||||||
|
html: function(){
|
||||||
|
res.send('<p>hey</p>');
|
||||||
|
},
|
||||||
|
|
||||||
|
json: function(){
|
||||||
|
res.send({ message: 'hey' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
8
en/api/res-get.jade
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.get') res.get(field)
|
||||||
|
p
|
||||||
|
| Get the case-insensitive response header <code>field</code>.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.get('Content-Type');
|
||||||
|
// => "text/plain"
|
||||||
14
en/api/res-json.jade
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.json') res.json([status|body], [body])
|
||||||
|
|
||||||
|
p.
|
||||||
|
Send a JSON response. This method is identical
|
||||||
|
to <code>res.send()</code> when an object or
|
||||||
|
array is passed, however it may be used for
|
||||||
|
explicit JSON conversion of non-objects (null, undefined, etc),
|
||||||
|
though these are technically not valid JSON.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.json(null)
|
||||||
|
res.json({ user: 'tobi' })
|
||||||
|
res.json(500, { error: 'message' })
|
||||||
19
en/api/res-links.jade
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.links') res.links(links)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Join the given <code>links</code> to populate the "Link"
|
||||||
|
response header field.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.links({
|
||||||
|
next: 'http://api.example.com/users?page=2',
|
||||||
|
last: 'http://api.example.com/users?page=5'
|
||||||
|
});
|
||||||
|
|
||||||
|
p yields:
|
||||||
|
|
||||||
|
pre
|
||||||
|
code.
|
||||||
|
Link: <http://api.example.com/users?page=2>; rel="next",
|
||||||
|
<http://api.example.com/users?page=5>; rel="last"
|
||||||
18
en/api/res-locals.jade
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.locals') res.locals
|
||||||
|
|
||||||
|
p.
|
||||||
|
Response local variables are scoped to the request, thus only
|
||||||
|
available to the view(s) rendered during that request / response
|
||||||
|
cycle, if any. Otherwise this API is identical to <a href="#app-locals">app.locals</a>.
|
||||||
|
|
||||||
|
p.
|
||||||
|
This object is useful for exposes request-level information such as the
|
||||||
|
request pathname, authenticated user, user settings etcetera.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.use(function(req, res, next){
|
||||||
|
res.locals.user = req.user;
|
||||||
|
res.locals.authenticated = ! req.user.anonymous;
|
||||||
|
next();
|
||||||
|
});
|
||||||
53
en/api/res-redirect.jade
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.redirect') res.redirect([status], url)
|
||||||
|
p.
|
||||||
|
Redirect to the given <code>url</code> with optional <code>statua</code> code
|
||||||
|
defaulting to 302 "Found".
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.redirect('/foo/bar');
|
||||||
|
res.redirect('http://example.com');
|
||||||
|
res.redirect(301, 'http://example.com');
|
||||||
|
res.redirect('../login');
|
||||||
|
|
||||||
|
p.
|
||||||
|
Express supports a few forms of redirection, first being
|
||||||
|
a fully qualified URI for redirecting to a different site:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.redirect('http://google.com');
|
||||||
|
|
||||||
|
p.
|
||||||
|
The second form is the pathname-relative redirect, for example
|
||||||
|
if you were on <code>http://example.com/admin/post/new</code>, the
|
||||||
|
following redirect to <code>/admin</code> would land you at <code>http://example.com/admin</code>:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.redirect('/admin');
|
||||||
|
|
||||||
|
p.
|
||||||
|
This next redirect is relative to the <code>mount</code> point of the application. For example
|
||||||
|
if you have a blog application mounted at <code>/blog</code>, ideally it has no knowledge of
|
||||||
|
where it was mounted, so where a redirect of <code>/admin/post/new</code> would simply give you
|
||||||
|
<code>http://example.com/admin/post/new</code>, the following mount-relative redirect would give
|
||||||
|
you <code>http://example.com/blog/admin/post/new</code>:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.redirect('admin/post/new');
|
||||||
|
|
||||||
|
p.
|
||||||
|
Pathname relative redirects are also possible. If you were
|
||||||
|
on <code>http://example.com/admin/post/new</code>, the following redirect
|
||||||
|
would land you at <code>http//example.com/admin/post</code>:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.redirect('..');
|
||||||
|
|
||||||
|
p.
|
||||||
|
The final special-case is a <code>back</code> redirect, redirecting back to
|
||||||
|
the Referer (or Referrer), defaulting to <code>/</code> when missing.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.redirect('back');
|
||||||
|
|
||||||
|
|
||||||
17
en/api/res-render.jade
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.render') res.render(view, [locals], callback)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Render a <code>view</code> with a callback responding with
|
||||||
|
the rendered string. When an error occurs <code>next(err)</code>
|
||||||
|
is invoked internally. When a callback is provided both the possible error
|
||||||
|
and rendered string are passed, and no automated response is performed.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.render('index', function(err, html){
|
||||||
|
// ...
|
||||||
|
});
|
||||||
|
|
||||||
|
app.render('user', { name: 'Tobi' }, function(err, html){
|
||||||
|
// ...
|
||||||
|
});
|
||||||
55
en/api/res-send.jade
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.send') res.send([body|status], [body])
|
||||||
|
|
||||||
|
p.
|
||||||
|
Send a response.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.send(new Buffer('whoop'));
|
||||||
|
res.send({ some: 'json' });
|
||||||
|
res.send('<p>some html</p>');
|
||||||
|
res.send(404, 'Sorry, we cannot find that!');
|
||||||
|
res.send(500, { error: 'something blew up' });
|
||||||
|
res.send(200);
|
||||||
|
|
||||||
|
p.
|
||||||
|
This method performs a myriad of
|
||||||
|
useful tasks for simple non-streaming responses such
|
||||||
|
as automatically assigning the Content-Length unless
|
||||||
|
previously defined and providing automatic <em>HEAD</em> and
|
||||||
|
HTTP cache freshness support.
|
||||||
|
|
||||||
|
p.
|
||||||
|
When a <code>Buffer</code> is given
|
||||||
|
the Content-Type is set to "application/octet-stream"
|
||||||
|
unless previously defined as shown below:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.set('Content-Type', 'text/html');
|
||||||
|
res.send(new Buffer('<p>some html</p>'));
|
||||||
|
|
||||||
|
p.
|
||||||
|
When a <code>String</code> is given the
|
||||||
|
Content-Type is set defaulted to "text/html":
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.send('<p>some html</p>');
|
||||||
|
|
||||||
|
p.
|
||||||
|
When an <code>Array</code> or <code>Object</code> is
|
||||||
|
given Express will respond with the JSON representation:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.send({ user: 'tobi' })
|
||||||
|
res.send([1,2,3])
|
||||||
|
|
||||||
|
p.
|
||||||
|
Finally when a <code>Number</code> is given without
|
||||||
|
any of the previously mentioned bodies, then a response
|
||||||
|
body string is assigned for you. For example 200 will
|
||||||
|
respond will the text "OK", and 204 "Not Found" and so on.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.send(200)
|
||||||
|
res.send(204)
|
||||||
|
res.send(500)
|
||||||
33
en/api/res-sendfile.jade
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.sendfile') res.sendfile(path, [options], [fn]])
|
||||||
|
|
||||||
|
p Transfer the file at the given <code>path</code>.
|
||||||
|
|
||||||
|
p.
|
||||||
|
Automatically defaults the Content-Type response header field based
|
||||||
|
on the filename's extension. The callback <code>fn(err)</code> is
|
||||||
|
invoked when the transfer is complete or when an error occurs.
|
||||||
|
|
||||||
|
p Options:
|
||||||
|
|
||||||
|
ul
|
||||||
|
li <code>maxAge</code> in milliseconds defaulting to 0
|
||||||
|
li <code>root</code> root directory for relative filenames
|
||||||
|
|
||||||
|
p.
|
||||||
|
This method provides fine-grained support for file serving
|
||||||
|
as illustrated in the following example:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.get('/user/:uid/photos/:file', function(req, res){
|
||||||
|
var uid = req.params.uid
|
||||||
|
, file = req.params.file;
|
||||||
|
|
||||||
|
req.user.mayViewFilesFrom(uid, function(yes){
|
||||||
|
if (yes) {
|
||||||
|
res.sendfile('/uploads/' + uid + '/' + file);
|
||||||
|
} else {
|
||||||
|
res.send(403, 'Sorry! you cant see that.');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
16
en/api/res-set.jade
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.set') res.set(field, [value])
|
||||||
|
p
|
||||||
|
| Set header <code>field</code> to <code>value</code>,
|
||||||
|
| or pass an object to set multiple fields at once.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.set('Content-Type', 'text/plain');
|
||||||
|
|
||||||
|
res.set({
|
||||||
|
'Content-Type': 'text/plain',
|
||||||
|
'Content-Length': '123',
|
||||||
|
'ETag': '12345'
|
||||||
|
})
|
||||||
|
|
||||||
|
p Aliased as <code>res.header(field, [value])</code>.
|
||||||
8
en/api/res-status.jade
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.status') res.status(code)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Chainable alias of node's '`res.statusCode=`.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
req.status(204).sendfile('path/to/404.png');
|
||||||
14
en/api/res-type.jade
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
section
|
||||||
|
h3(id='res.type') res.type(type)
|
||||||
|
|
||||||
|
p.
|
||||||
|
Sets the Content-Type to the mime lookup of <code>type</code>,
|
||||||
|
or when "/" is present the Content-Type is simply set to this
|
||||||
|
literal value.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
res.type('.html');
|
||||||
|
res.type('html');
|
||||||
|
res.type('json');
|
||||||
|
res.type('application/json');
|
||||||
|
res.type('png');
|
||||||
17
en/api/res.jade
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
include ./res-status
|
||||||
|
include ./res-set
|
||||||
|
include ./res-get
|
||||||
|
include ./res-cookie
|
||||||
|
include ./res-clearCookie
|
||||||
|
include ./res-redirect
|
||||||
|
include ./res-charset
|
||||||
|
include ./res-send
|
||||||
|
include ./res-json
|
||||||
|
include ./res-type
|
||||||
|
include ./res-format
|
||||||
|
include ./res-attachment
|
||||||
|
include ./res-sendfile
|
||||||
|
include ./res-download
|
||||||
|
include ./res-links
|
||||||
|
include ./res-locals
|
||||||
|
include ./res-render
|
||||||
86
en/applications.jade
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
|
||||||
|
mixin image(name)
|
||||||
|
img(src='/images/apps/screenshots/#{name}.small.png')
|
||||||
|
|
||||||
|
mixin link(title, url)
|
||||||
|
.link
|
||||||
|
| Visit
|
||||||
|
a(href=url)= title
|
||||||
|
|
||||||
|
section.application
|
||||||
|
h2 LearnBoost
|
||||||
|
p.
|
||||||
|
LearnBoost provides a free, easy to use online
|
||||||
|
online education suite including gradebook,
|
||||||
|
lesson plans, attendance, reporting, and calendars
|
||||||
|
among other tools.
|
||||||
|
|
||||||
|
+link('LearnBoost', 'https://www.learnboost.com/')
|
||||||
|
+image('learnboost')
|
||||||
|
|
||||||
|
section.application
|
||||||
|
h2 Storify
|
||||||
|
p.
|
||||||
|
Create stories using social media. Turn what people post
|
||||||
|
on social media into compelling stories. Collect the best photos, video,
|
||||||
|
tweets and more to publish
|
||||||
|
|
||||||
|
+link('Storify', 'http://storify.com/')
|
||||||
|
+image('storify')
|
||||||
|
|
||||||
|
section.application
|
||||||
|
h2 Geekli.st
|
||||||
|
p.
|
||||||
|
A place for geeks to share what they've done, who they did it with and
|
||||||
|
connect with great companies and communities.
|
||||||
|
|
||||||
|
+link('Geekli.st', 'http://geekli.st')
|
||||||
|
+image('geeklist')
|
||||||
|
|
||||||
|
section.application
|
||||||
|
h2 Klout
|
||||||
|
p.
|
||||||
|
Klout is the Standard for Influence. Join Klout to discover your
|
||||||
|
influence and compare with others you may know.
|
||||||
|
|
||||||
|
+link('Klout', 'http://klout.com')
|
||||||
|
+image('klout')
|
||||||
|
|
||||||
|
section.application
|
||||||
|
h2 Prismatic
|
||||||
|
p.
|
||||||
|
Prismatic learns from how you interact on social networks so that we
|
||||||
|
can show you the most interesting content and conversation from your friends.
|
||||||
|
|
||||||
|
+link('Prismatic', 'http://getprismatic.com/')
|
||||||
|
+image('prismatic')
|
||||||
|
|
||||||
|
section.application
|
||||||
|
h2 Clipboard
|
||||||
|
p.
|
||||||
|
Clipboard is a powerful tool allowing to save live clips
|
||||||
|
of your interests on the web, not just static images,
|
||||||
|
but fully-functional fragments of anything online.
|
||||||
|
|
||||||
|
+link('Clipboard', 'http://clipboard.com/')
|
||||||
|
+image('clipboard')
|
||||||
|
|
||||||
|
section.application
|
||||||
|
h2 Persona
|
||||||
|
p.
|
||||||
|
Persona, or "BrowserID" is Mozilla's answer
|
||||||
|
to a better identification system for your browser,
|
||||||
|
this promising tool is definitely worth checking out.
|
||||||
|
|
||||||
|
+link('Persona', 'https://login.persona.org/')
|
||||||
|
+image('browserid')
|
||||||
|
|
||||||
|
section.application
|
||||||
|
h2 and more!
|
||||||
|
p.
|
||||||
|
Shodan search reports that there are well over <strong>26,000</strong> Express applications
|
||||||
|
in the wild, we can't possibly list them all here, but if you feel
|
||||||
|
your application helps showcase the framework open an issue on
|
||||||
|
the <a href="github.com/visionmedia/expressjs.com/issues">github repo</a>.
|
||||||
|
|
||||||
|
+image('more')
|
||||||
34
en/community.jade
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#boxes.clearfix
|
||||||
|
section#mailing-list
|
||||||
|
h3 Mailing List
|
||||||
|
p.
|
||||||
|
Join over 1500 Express users or browse over 5000
|
||||||
|
discussions in the <a href="https://groups.google.com/group/express-js">Google Group</a>.
|
||||||
|
|
||||||
|
section#irc
|
||||||
|
h3 IRC Channel
|
||||||
|
p.
|
||||||
|
Hundreds of developers idle in #express on freenode every day,
|
||||||
|
if you have questions about the framework jump in for quick
|
||||||
|
feedback.
|
||||||
|
|
||||||
|
section#examples
|
||||||
|
h3 Examples
|
||||||
|
p.
|
||||||
|
View dozens of Express application <a href="https://github.com/visionmedia/express/tree/master/examples">examples</a>
|
||||||
|
in the github repository covering everything from API design and authentication
|
||||||
|
to template engine integration.
|
||||||
|
|
||||||
|
section#issues
|
||||||
|
h3 Issues
|
||||||
|
p.
|
||||||
|
If you've come across what you think is a bug, or just want to make
|
||||||
|
a feature request open a ticket in the <a href="https://github.com/visionmedia/express/issues">issue queue</a>.
|
||||||
|
|
||||||
|
section#extending
|
||||||
|
h3 Third Party
|
||||||
|
p.
|
||||||
|
Our vibrant community has created a large variety of extensions,
|
||||||
|
<a href="https://github.com/senchalabs/connect/wiki">middleware</a>
|
||||||
|
and higher level frameworks. Check them out in the
|
||||||
|
<a href="https://github.com/visionmedia/express/wiki">wiki</a>.
|
||||||
117
en/faq.jade
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
|
||||||
|
section
|
||||||
|
h3(id='models') How do I define models?
|
||||||
|
|
||||||
|
p.
|
||||||
|
Express has no notion of a database at all, this is
|
||||||
|
left up to third-party node modules - allowing you to
|
||||||
|
interface with nearly any database.
|
||||||
|
|
||||||
|
section
|
||||||
|
h3(id='auth') How can I authenticate users?
|
||||||
|
|
||||||
|
p.
|
||||||
|
This is another opinionated area that Express does not
|
||||||
|
venture into, you may use any authentication scheme you wish.
|
||||||
|
For a simple username / password scheme view this <a href="https://github.com/visionmedia/express/tree/master/examples/auth">example</a>.
|
||||||
|
|
||||||
|
section
|
||||||
|
h3(id='templates') Which template engines does Express support?
|
||||||
|
|
||||||
|
p.
|
||||||
|
Anything that can conform with the <code>(path, locals, callback)</code> signature.
|
||||||
|
To normalize template engine interfaces and caching it's recommended to
|
||||||
|
check the <a href="https://github.com/visionmedia/consolidate.js">consolidate.js</a>
|
||||||
|
project for support. Unlisted template engines may still support the Express
|
||||||
|
signature.
|
||||||
|
|
||||||
|
section
|
||||||
|
h3(id='structure') How should I structure my application?
|
||||||
|
|
||||||
|
p.
|
||||||
|
There is no true answer to this question, it is highly dependant
|
||||||
|
on the scale of your application and the team involved. To be as
|
||||||
|
flexible as possible Express makes no assumptions in terms of structure.
|
||||||
|
|
||||||
|
p.
|
||||||
|
Routes and other application-specific logic may live in as many files
|
||||||
|
as you wish, in any directory structure you prefer. View the following
|
||||||
|
examples for inspiration:
|
||||||
|
|
||||||
|
ul
|
||||||
|
li: a(href='https://github.com/visionmedia/express/blob/master/examples/route-separation/app.js#L20') Route listings
|
||||||
|
li: a(href='https://github.com/visionmedia/express/blob/master/examples/route-map/index.js#L47') Route map
|
||||||
|
li: a(href='https://github.com/visionmedia/express/tree/master/examples/route-loading') Route bootstrapping
|
||||||
|
li: a(href='https://github.com/visionmedia/express/tree/master/examples/mvc') MVC style controllers
|
||||||
|
|
||||||
|
p.
|
||||||
|
Available as well are third-party extensions to Express to simplify some of these patterns:
|
||||||
|
|
||||||
|
ul
|
||||||
|
li: a(href='https://github.com/visionmedia/express-resource') Resourceful routing
|
||||||
|
li: a(href='https://github.com/visionmedia/express-namespace') Namespaced routing
|
||||||
|
|
||||||
|
section
|
||||||
|
h3(id='multiple-statics') How can I serve statics from several directories?
|
||||||
|
|
||||||
|
p.
|
||||||
|
You may typically use any middleware several times
|
||||||
|
within your application. With the following middleware setup and a request
|
||||||
|
to "GET /javascripts/jquery.js" would first check "./public/javascripts/jquery.js",
|
||||||
|
if it does not exist then the subsequent middleware will check "./files/javascripts/jquery.js".
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.use(express.static('public'));
|
||||||
|
app.use(express.static('files'));
|
||||||
|
|
||||||
|
setup
|
||||||
|
h3(id='static-prefix') How can I prefix a pathname for serving statics?
|
||||||
|
|
||||||
|
p.
|
||||||
|
Connect's generic "mounting" feature allows you to define
|
||||||
|
the pathname "prefix" to which the middleware will be invoked,
|
||||||
|
effectively behaving as if that prefix string was never
|
||||||
|
part of the path. Suppose if you wanted "GET /files/javascripts/jquery.js",
|
||||||
|
you could mount the middleware at "/files", exposing "/javascripts/jquery.js"
|
||||||
|
as the <code>req.url</code> allowing the middleware to serve the file:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.use('/public', express.static('public'));
|
||||||
|
|
||||||
|
setup
|
||||||
|
h3(id='migration') How do I migrate my Express 2.x application?
|
||||||
|
|
||||||
|
p.
|
||||||
|
Express 2x will likely be supported through to node 1.0 so there's
|
||||||
|
no immediate reason to update beyond the refactoring and API changes
|
||||||
|
that Express 3x introduces, so if you're happy with 2x feel free
|
||||||
|
to remain on that branch. For migration information visit the
|
||||||
|
<a href="https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x">migration</a>
|
||||||
|
wiki page, or view a <a href="https://github.com/visionmedia/express/wiki/New-features-in-3.x">list of changes</a> made in 3.x.
|
||||||
|
|
||||||
|
setup
|
||||||
|
h3(id='error-handling') How do you setup an error handler in Express?
|
||||||
|
|
||||||
|
p.
|
||||||
|
Error-handling middleware are defined just like regular middleware,
|
||||||
|
however must be define with an arity of 4, that is the signature
|
||||||
|
<code>(err, req, res, next)</code>:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.use(function(err, req, res, next){
|
||||||
|
console.error(err.stack);
|
||||||
|
res.send(500, 'Something broke!');
|
||||||
|
});
|
||||||
|
|
||||||
|
p.
|
||||||
|
View <a href="http://localhost:3000/guide.html#error-handling">error-handling</a> information.
|
||||||
|
|
||||||
|
setup
|
||||||
|
h3(id='size') How big is the Express codebase?
|
||||||
|
|
||||||
|
p.
|
||||||
|
Express is a very small framework, the 3.0.0 release is only
|
||||||
|
932 SLOC, and the mandatory portion of Connect which Express
|
||||||
|
is built on is only 267 SLOC. The optional middleware bundled
|
||||||
|
with Connect add an additional 1143 SLOC, and are lazy loaded
|
||||||
|
upon use.
|
||||||
76
en/guide/executable.jade
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
section
|
||||||
|
h3(id='executable') Using express(1) to generate an app
|
||||||
|
|
||||||
|
p.
|
||||||
|
Express is bundled with an executable, aptly named <code>express(1)</code>.
|
||||||
|
If you install express globally with npm you'll have it available from anywhere
|
||||||
|
on your machine:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
$ npm install -g express
|
||||||
|
|
||||||
|
p.
|
||||||
|
This tool provides a simple way to get an application skeleton going,
|
||||||
|
but has limited scope, for example it supports only a few template engines,
|
||||||
|
whereas Express itself supports virtually any template engine built for node.
|
||||||
|
Be sure to check out the <code>--help</code>:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
|
||||||
|
Usage: express [options]
|
||||||
|
|
||||||
|
Options:
|
||||||
|
|
||||||
|
-h, --help output usage information
|
||||||
|
-V, --version output the version number
|
||||||
|
-s, --sessions add session support
|
||||||
|
-e, --ejs add ejs engine support (defaults to jade)
|
||||||
|
-J, --jshtml add jshtml engine support (defaults to jade)
|
||||||
|
-h, --hogan add hogan.js engine support
|
||||||
|
-c, --css <engine> add stylesheet <engine> support (less|stylus) (defaults to plain css)
|
||||||
|
-f, --force force on non-empty directory
|
||||||
|
p.
|
||||||
|
If you want to generate an application with EJS, Stylus, and session
|
||||||
|
support you would simply execute:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
$ express --sessions --css stylus --ejs myapp
|
||||||
|
|
||||||
|
create : myapp
|
||||||
|
create : myapp/package.json
|
||||||
|
create : myapp/app.js
|
||||||
|
create : myapp/public
|
||||||
|
create : myapp/public/javascripts
|
||||||
|
create : myapp/public/images
|
||||||
|
create : myapp/public/stylesheets
|
||||||
|
create : myapp/public/stylesheets/style.styl
|
||||||
|
create : myapp/routes
|
||||||
|
create : myapp/routes/index.js
|
||||||
|
create : myapp/views
|
||||||
|
create : myapp/views/index.ejs
|
||||||
|
|
||||||
|
install dependencies:
|
||||||
|
$ cd myapp && npm install
|
||||||
|
|
||||||
|
run the app:
|
||||||
|
$ node app
|
||||||
|
|
||||||
|
p.
|
||||||
|
Like any other node application, you must then install the dependencies:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
$ cd myapp
|
||||||
|
$ npm install
|
||||||
|
|
||||||
|
p.
|
||||||
|
Then fire it up!
|
||||||
|
|
||||||
|
+js.
|
||||||
|
$ node app
|
||||||
|
|
||||||
|
p.
|
||||||
|
That's all you need to get a simple application up and running. Keep in mind
|
||||||
|
that Express is not bound to any specific directory structure, these are simply
|
||||||
|
a baseline for you to work from. For application structure alternatives be
|
||||||
|
sure to view the <a href="https://github.com/visionmedia/express/tree/master/examples">examples</a>
|
||||||
|
found in the github repo.
|
||||||
2
en/guide/index.jade
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
include intro
|
||||||
|
include executable
|
||||||
90
en/guide/intro.jade
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
section
|
||||||
|
h3(id='intro') Getting started
|
||||||
|
|
||||||
|
p.
|
||||||
|
With node installed (<a href="http://nodejs.org/#download">download</a>),
|
||||||
|
get your first application started by creating a directory somewhere
|
||||||
|
on your machine:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
$ mkdir hello-world
|
||||||
|
|
||||||
|
p.
|
||||||
|
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:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
{
|
||||||
|
"name": "hello-world",
|
||||||
|
"description": "hello world test app",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"express": "3.0.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
p.
|
||||||
|
Now that you have a package.json file in this directory you can use
|
||||||
|
<code>npm(1)</code> to install the dependencies, in this case just
|
||||||
|
Express:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
$ npm install
|
||||||
|
|
||||||
|
p.
|
||||||
|
Once npm finishes you'll have a localized Express 3.x dependency in
|
||||||
|
the ./node_modules directory. You may verify this with <code>npm ls</code>
|
||||||
|
as shown in the following snippet displaying a tree of Express and its
|
||||||
|
own dependencies.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
$ npm ls
|
||||||
|
hello-world@0.0.1 /private/tmp
|
||||||
|
└─┬ express@3.0.0beta7
|
||||||
|
├── commander@0.6.1
|
||||||
|
├─┬ connect@2.3.9
|
||||||
|
│ ├── bytes@0.1.0
|
||||||
|
│ ├── cookie@0.0.4
|
||||||
|
│ ├── crc@0.2.0
|
||||||
|
│ ├── formidable@1.0.11
|
||||||
|
│ └── qs@0.4.2
|
||||||
|
├── cookie@0.0.3
|
||||||
|
├── debug@0.7.0
|
||||||
|
├── fresh@0.1.0
|
||||||
|
├── methods@0.0.1
|
||||||
|
├── mkdirp@0.3.3
|
||||||
|
├── range-parser@0.0.4
|
||||||
|
├─┬ response-send@0.0.1
|
||||||
|
│ └── crc@0.2.0
|
||||||
|
└─┬ send@0.0.3
|
||||||
|
└── mime@1.2.6
|
||||||
|
|
||||||
|
p.
|
||||||
|
Now to create the application itself! Create a file named app.js or server.js,
|
||||||
|
whichever you prefer, require express and then create a new application with <code>express()</code>:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
var express = require('express');
|
||||||
|
var app = express();
|
||||||
|
|
||||||
|
p.
|
||||||
|
With the new application instance you can start defining routes via <code>app.VERB()</code>,
|
||||||
|
in this case "GET /" responding with the "Hello World" string. The <code>req</code> and
|
||||||
|
<code>res</code> are the exact same objects that node provides to you, thus you may invoke
|
||||||
|
<code>res.pipe()</code>, <code>req.on('data', callback)</code> and anything else you
|
||||||
|
would do without Express involved.
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.get('/', function(req, res){
|
||||||
|
res.send('Hello World');
|
||||||
|
});
|
||||||
|
|
||||||
|
p.
|
||||||
|
Now to bind and listen for connections invoke the <code>app.listen()</code> method,
|
||||||
|
accepting the same arguments as node's <a href="http://nodejs.org/api/net.html#net_server_listen_port_host_backlog_listeninglistener">net.Server#listen()</a>:
|
||||||
|
|
||||||
|
+js.
|
||||||
|
app.listen(3000);
|
||||||
|
console.log('Listening on port 3000');
|
||||||
4
en/guide/menu.jade
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
ul#menu
|
||||||
|
li: ul
|
||||||
|
li: a(href='#intro') Getting started
|
||||||
|
li: a(href='#executable') express(1) executable
|
||||||
51
faq.html
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
<!DOCTYPE html><html><head><title>Express - faq</title><link rel="stylesheet" href="style.css"><link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700&subset=latin,latin-ext"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script><script src="app.js"></script></head><body class="inner"><div class="bar"></div><section id="content"><header><section id="logo"><span class="express">express<em>3.0.0</em></span><span class="description">
|
||||||
|
web application framework for <a href="http://nodejs.org">node </a></span></section><nav class="clearfix"><a href="/" class=""> Home</a><a href="/api.html" class=""> API Reference</a><a href="/guide.html" class=""> Guide</a><a href="/applications.html" class=""> Applications</a><a href="/community.html" class=""> Community</a><a href="/faq.html" class="active"> FAQ</a></nav><div id="x"></div><div id="y"></div></header><div id="faq"><section><h3 id="models">How do I define models?</h3><p>Express has no notion of a database at all, this is
|
||||||
|
left up to third-party node modules - allowing you to
|
||||||
|
interface with nearly any database.
|
||||||
|
</p></section><section><h3 id="auth">How can I authenticate users?</h3><p>This is another opinionated area that Express does not
|
||||||
|
venture into, you may use any authentication scheme you wish.
|
||||||
|
For a simple username / password scheme view this <a href="https://github.com/visionmedia/express/tree/master/examples/auth">example</a>.
|
||||||
|
</p></section><section><h3 id="templates">Which template engines does Express support?</h3><p>Anything that can conform with the <code>(path, locals, callback)</code> signature.
|
||||||
|
To normalize template engine interfaces and caching it's recommended to
|
||||||
|
check the <a href="https://github.com/visionmedia/consolidate.js">consolidate.js</a>
|
||||||
|
project for support. Unlisted template engines may still support the Express
|
||||||
|
signature.
|
||||||
|
</p></section><section><h3 id="structure">How should I structure my application?</h3><p>There is no true answer to this question, it is highly dependant
|
||||||
|
on the scale of your application and the team involved. To be as
|
||||||
|
flexible as possible Express makes no assumptions in terms of structure.
|
||||||
|
</p><p>Routes and other application-specific logic may live in as many files
|
||||||
|
as you wish, in any directory structure you prefer. View the following
|
||||||
|
examples for inspiration:
|
||||||
|
</p><ul><li><a href="https://github.com/visionmedia/express/blob/master/examples/route-separation/app.js#L20">Route listings</a></li><li><a href="https://github.com/visionmedia/express/blob/master/examples/route-map/index.js#L47">Route map</a></li><li><a href="https://github.com/visionmedia/express/tree/master/examples/route-loading">Route bootstrapping</a></li><li><a href="https://github.com/visionmedia/express/tree/master/examples/mvc">MVC style controllers</a></li></ul><p>Available as well are third-party extensions to Express to simplify some of these patterns:
|
||||||
|
</p><ul><li><a href="https://github.com/visionmedia/express-resource">Resourceful routing</a></li><li><a href="https://github.com/visionmedia/express-namespace">Namespaced routing</a></li></ul></section><section><h3 id="multiple-statics">How can I serve statics from several directories?</h3><p>You may typically use any middleware several times
|
||||||
|
within your application. With the following middleware setup and a request
|
||||||
|
to "GET /javascripts/jquery.js" would first check "./public/javascripts/jquery.js",
|
||||||
|
if it does not exist then the subsequent middleware will check "./files/javascripts/jquery.js".
|
||||||
|
</p><pre class="js"><code>app.use(express.static('public'));
|
||||||
|
app.use(express.static('files'));
|
||||||
|
</code></pre></section><setup><h3 id="static-prefix">How can I prefix a pathname for serving statics?</h3><p>Connect's generic "mounting" feature allows you to define
|
||||||
|
the pathname "prefix" to which the middleware will be invoked,
|
||||||
|
effectively behaving as if that prefix string was never
|
||||||
|
part of the path. Suppose if you wanted "GET /files/javascripts/jquery.js",
|
||||||
|
you could mount the middleware at "/files", exposing "/javascripts/jquery.js"
|
||||||
|
as the <code>req.url</code> allowing the middleware to serve the file:
|
||||||
|
</p><pre class="js"><code>app.use('/public', express.static('public'));
|
||||||
|
</code></pre></setup><setup><h3 id="migration">How do I migrate my Express 2.x application?</h3><p>Express 2x will likely be supported through to node 1.0 so there's
|
||||||
|
no immediate reason to update beyond the refactoring and API changes
|
||||||
|
that Express 3x introduces, so if you're happy with 2x feel free
|
||||||
|
to remain on that branch. For migration information visit the
|
||||||
|
<a href="https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x">migration</a>
|
||||||
|
wiki page, or view a <a href="https://github.com/visionmedia/express/wiki/New-features-in-3.x">list of changes</a> made in 3.x.
|
||||||
|
</p></setup><setup><h3 id="error-handling">How do you setup an error handler in Express?</h3><p>Error-handling middleware are defined just like regular middleware,
|
||||||
|
however must be define with an arity of 4, that is the signature
|
||||||
|
<code>(err, req, res, next)</code>:
|
||||||
|
</p><pre class="js"><code>app.use(function(err, req, res, next){
|
||||||
|
console.error(err.stack);
|
||||||
|
res.send(500, 'Something broke!');
|
||||||
|
});
|
||||||
|
</code></pre><p>View <a href="http://localhost:3000/guide.html#error-handling">error-handling</a> information.
|
||||||
|
</p></setup><setup><h3 id="size">How big is the Express codebase?</h3><p>Express is a very small framework, the 3.0.0 release is only
|
||||||
|
932 SLOC, and the mandatory portion of Connect which Express
|
||||||
|
is built on is only 267 SLOC. The optional middleware bundled
|
||||||
|
with Connect add an additional 1143 SLOC, and are lazy loaded
|
||||||
|
upon use.</p></setup></div></section><a id="top" href="#"><img src="images/arrow.png"></a><footer><div id="footer-content">© 2012 TJ Holowaychuk. All rights reserved.</div></footer></body></html>
|
||||||
15
faq.jade
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
!!! 5
|
||||||
|
html
|
||||||
|
head
|
||||||
|
title Express - faq
|
||||||
|
include includes/head
|
||||||
|
body.inner
|
||||||
|
.bar
|
||||||
|
section#content
|
||||||
|
header
|
||||||
|
include includes/logo
|
||||||
|
active = '/faq.html'
|
||||||
|
include includes/menu
|
||||||
|
#faq
|
||||||
|
include en/faq
|
||||||
|
include includes/footer
|
||||||
111
guide.html
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
<!DOCTYPE html><html><head><title>Express - api reference</title><link rel="stylesheet" href="style.css"><link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700&subset=latin,latin-ext"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script><script src="app.js"></script></head><body class="inner"><div class="bar"></div><section id="content"><header><section id="logo"><span class="express">express<em>3.0.0</em></span><span class="description">
|
||||||
|
web application framework for <a href="http://nodejs.org">node </a></span></section><nav class="clearfix"><a href="/" class=""> Home</a><a href="/api.html" class=""> API Reference</a><a href="/guide.html" class="active"> Guide</a><a href="/applications.html" class=""> Applications</a><a href="/community.html" class=""> Community</a><a href="/faq.html" class=""> FAQ</a></nav><div id="x"></div><div id="y"></div></header><ul id="menu"><li><ul><li><a href="#intro">Getting started</a></li><li><a href="#executable">express(1) executable</a></li></ul></li></ul><section><h3 id="intro">Getting started</h3><p>With node installed (<a href="http://nodejs.org/#download">download</a>),
|
||||||
|
get your first application started by creating a directory somewhere
|
||||||
|
on your machine:
|
||||||
|
</p><pre class="js"><code>$ mkdir hello-world
|
||||||
|
</code></pre><p>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:
|
||||||
|
</p><pre class="js"><code>{
|
||||||
|
"name": "hello-world",
|
||||||
|
"description": "hello world test app",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"express": "3.0.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</code></pre><p>Now that you have a package.json file in this directory you can use
|
||||||
|
<code>npm(1)</code> to install the dependencies, in this case just
|
||||||
|
Express:
|
||||||
|
</p><pre class="js"><code>$ npm install
|
||||||
|
</code></pre><p>Once npm finishes you'll have a localized Express 3.x dependency in
|
||||||
|
the ./node_modules directory. You may verify this with <code>npm ls</code>
|
||||||
|
as shown in the following snippet displaying a tree of Express and its
|
||||||
|
own dependencies.
|
||||||
|
</p><pre class="js"><code>$ npm ls
|
||||||
|
hello-world@0.0.1 /private/tmp
|
||||||
|
└─┬ express@3.0.0beta7
|
||||||
|
├── commander@0.6.1
|
||||||
|
├─┬ connect@2.3.9
|
||||||
|
│ ├── bytes@0.1.0
|
||||||
|
│ ├── cookie@0.0.4
|
||||||
|
│ ├── crc@0.2.0
|
||||||
|
│ ├── formidable@1.0.11
|
||||||
|
│ └── qs@0.4.2
|
||||||
|
├── cookie@0.0.3
|
||||||
|
├── debug@0.7.0
|
||||||
|
├── fresh@0.1.0
|
||||||
|
├── methods@0.0.1
|
||||||
|
├── mkdirp@0.3.3
|
||||||
|
├── range-parser@0.0.4
|
||||||
|
├─┬ response-send@0.0.1
|
||||||
|
│ └── crc@0.2.0
|
||||||
|
└─┬ send@0.0.3
|
||||||
|
└── mime@1.2.6
|
||||||
|
</code></pre><p>Now to create the application itself! Create a file named app.js or server.js,
|
||||||
|
whichever you prefer, require express and then create a new application with <code>express()</code>:
|
||||||
|
</p><pre class="js"><code>var express = require('express');
|
||||||
|
var app = express();
|
||||||
|
</code></pre><p>With the new application instance you can start defining routes via <code>app.VERB()</code>,
|
||||||
|
in this case "GET /" responding with the "Hello World" string. The <code>req</code> and
|
||||||
|
<code>res</code> are the exact same objects that node provides to you, thus you may invoke
|
||||||
|
<code>res.pipe()</code>, <code>req.on('data', callback)</code> and anything else you
|
||||||
|
would do without Express involved.
|
||||||
|
</p><pre class="js"><code>app.get('/', function(req, res){
|
||||||
|
res.send('Hello World');
|
||||||
|
});
|
||||||
|
</code></pre><p>Now to bind and listen for connections invoke the <code>app.listen()</code> method,
|
||||||
|
accepting the same arguments as node's <a href="http://nodejs.org/api/net.html#net_server_listen_port_host_backlog_listeninglistener">net.Server#listen()</a>:
|
||||||
|
</p><pre class="js"><code>app.listen(3000);
|
||||||
|
console.log('Listening on port 3000');</code></pre></section><section><h3 id="executable">Using express(1) to generate an app</h3><p>Express is bundled with an executable, aptly named <code>express(1)</code>.
|
||||||
|
If you install express globally with npm you'll have it available from anywhere
|
||||||
|
on your machine:
|
||||||
|
</p><pre class="js"><code>$ npm install -g express
|
||||||
|
</code></pre><p>This tool provides a simple way to get an application skeleton going,
|
||||||
|
but has limited scope, for example it supports only a few template engines,
|
||||||
|
whereas Express itself supports virtually any template engine built for node.
|
||||||
|
Be sure to check out the <code>--help</code>:
|
||||||
|
</p><pre class="js"><code>Usage: express [options]
|
||||||
|
|
||||||
|
Options:
|
||||||
|
|
||||||
|
-h, --help output usage information
|
||||||
|
-V, --version output the version number
|
||||||
|
-s, --sessions add session support
|
||||||
|
-e, --ejs add ejs engine support (defaults to jade)
|
||||||
|
-J, --jshtml add jshtml engine support (defaults to jade)
|
||||||
|
-h, --hogan add hogan.js engine support
|
||||||
|
-c, --css <engine> add stylesheet <engine> support (less|stylus) (defaults to plain css)
|
||||||
|
-f, --force force on non-empty directory</code></pre><p>If you want to generate an application with EJS, Stylus, and session
|
||||||
|
support you would simply execute:
|
||||||
|
</p><pre class="js"><code>$ express --sessions --css stylus --ejs myapp
|
||||||
|
|
||||||
|
create : myapp
|
||||||
|
create : myapp/package.json
|
||||||
|
create : myapp/app.js
|
||||||
|
create : myapp/public
|
||||||
|
create : myapp/public/javascripts
|
||||||
|
create : myapp/public/images
|
||||||
|
create : myapp/public/stylesheets
|
||||||
|
create : myapp/public/stylesheets/style.styl
|
||||||
|
create : myapp/routes
|
||||||
|
create : myapp/routes/index.js
|
||||||
|
create : myapp/views
|
||||||
|
create : myapp/views/index.ejs
|
||||||
|
|
||||||
|
install dependencies:
|
||||||
|
$ cd myapp && npm install
|
||||||
|
|
||||||
|
run the app:
|
||||||
|
$ node app
|
||||||
|
</code></pre><p>Like any other node application, you must then install the dependencies:
|
||||||
|
</p><pre class="js"><code>$ cd myapp
|
||||||
|
$ npm install
|
||||||
|
</code></pre><p>Then fire it up!
|
||||||
|
</p><pre class="js"><code>$ node app
|
||||||
|
</code></pre><p>That's all you need to get a simple application up and running. Keep in mind
|
||||||
|
that Express is not bound to any specific directory structure, these are simply
|
||||||
|
a baseline for you to work from. For application structure alternatives be
|
||||||
|
sure to view the <a href="https://github.com/visionmedia/express/tree/master/examples">examples</a>
|
||||||
|
found in the github repo.</p></section></section><a id="top" href="#"><img src="images/arrow.png"></a><footer><div id="footer-content">© 2012 TJ Holowaychuk. All rights reserved.</div></footer></body></html>
|
||||||
19
guide.jade
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
!!! 5
|
||||||
|
html
|
||||||
|
head
|
||||||
|
title Express - api reference
|
||||||
|
include includes/head
|
||||||
|
body.inner
|
||||||
|
.bar
|
||||||
|
section#content
|
||||||
|
header
|
||||||
|
include includes/logo
|
||||||
|
active = '/guide.html'
|
||||||
|
include includes/menu
|
||||||
|
|
||||||
|
include includes/mixins
|
||||||
|
include en/guide/menu
|
||||||
|
|
||||||
|
include en/guide/index
|
||||||
|
|
||||||
|
include includes/footer
|
||||||
BIN
images/apps/logos/canadian-tire.png
Normal file
|
After Width: | Height: | Size: 10 KiB |
BIN
images/apps/logos/clipboard.png
Normal file
|
After Width: | Height: | Size: 6.9 KiB |
BIN
images/apps/logos/doodle-or-die.png
Normal file
|
After Width: | Height: | Size: 5.9 KiB |
BIN
images/apps/logos/ebay.png
Normal file
|
After Width: | Height: | Size: 5.8 KiB |
BIN
images/apps/logos/geeklist.png
Normal file
|
After Width: | Height: | Size: 9.3 KiB |
BIN
images/apps/logos/klout.png
Normal file
|
After Width: | Height: | Size: 6.2 KiB |
BIN
images/apps/logos/koding.png
Normal file
|
After Width: | Height: | Size: 7.1 KiB |
BIN
images/apps/logos/learnboost.png
Normal file
|
After Width: | Height: | Size: 7.9 KiB |
BIN
images/apps/logos/mcdonalds.png
Normal file
|
After Width: | Height: | Size: 6.1 KiB |
BIN
images/apps/logos/mozilla.png
Normal file
|
After Width: | Height: | Size: 3.0 KiB |
BIN
images/apps/logos/nerve.png
Normal file
|
After Width: | Height: | Size: 5.2 KiB |
BIN
images/apps/logos/starbucks.png
Normal file
|
After Width: | Height: | Size: 11 KiB |
BIN
images/apps/logos/storify.png
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
images/apps/logos/uber.png
Normal file
|
After Width: | Height: | Size: 3.7 KiB |
BIN
images/apps/logos/yahoo.png
Normal file
|
After Width: | Height: | Size: 7.8 KiB |
BIN
images/apps/screenshots/browserid.small.png
Normal file
|
After Width: | Height: | Size: 129 KiB |
BIN
images/apps/screenshots/clipboard.small.png
Normal file
|
After Width: | Height: | Size: 243 KiB |