index.js
This file
Path
examples/static-files/index.js
40 lines · 1.3 KiB · JavaScript
Last changed by
Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff
History of this fileTake it
The walk stopped after 200 commits: lines older than 34c83d7 point at it.
Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff
lines 1–3why, one step back1/**2 * Module dependencies.3 */3.4.7Jonathan Ongbefore adoption34c83d7
lines 4–5why, one step back45var express = require('../..');remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08
line 6why, one step back6var logger = require('morgan');3.4.7Jonathan Ongbefore adoption34c83d7
lines 7–9why, one step back7var app = express();89// log requestsremove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08
line 10why, one step back10app.use(logger('dev'));3.4.7Jonathan Ongbefore adoption34c83d7
lines 11–40why, one step back1112// express on its own has no notion13// of a "file". The express.static()14// middleware checks for a file matching15// the `req.path` within the directory16// that you pass it. In this case "GET /js/app.js"17// will look for "./public/js/app.js".1819app.use(express.static(__dirname + '/public'));2021// if you wanted to "prefix" you may use22// the mounting feature of Connect, for example23// "GET /static/js/app.js" instead of "GET /js/app.js".24// The mount-path "/static" is simply removed before25// passing control to the express.static() middleware,26// thus it serves the file correctly by ignoring "/static"27app.use('/static', express.static(__dirname + '/public'));2829// if for some reason you want to serve files from30// several directories, you can use express.static()31// multiple times! Here we're passing "./public/css",32// this will allow "GET /style.css" instead of "GET /css/style.css":33app.use(express.static(__dirname + '/public/css'));3435app.listen(3000);36console.log('listening on port 3000');37console.log('try:');38console.log(' GET /hello.txt');39console.log(' GET /js/app.js');40console.log(' GET /css/style.css');