index.js
This file
Path
examples/static-files/index.js
40 lines · 1.3 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 6bc7574 point at it.
docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 1–40why, one step back1/**2 * Module dependencies.3 */45var express = require('../..');6var logger = require('morgan');7var app = express();89// log requests10app.use(logger('dev'));1112// 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');