index.js
This file
Path
examples/static-files/index.js
43 lines · 1.3 KiB · JavaScript
Last changed by
fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
History of this fileTake it
The walk stopped after 200 commits: lines older than 2027b87 point at it.
fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87
lines 1–8why, one step back1'use strict'23/**4 * Module dependencies.5 */67var express = require('../..');8var logger = require('morgan');fix(refactor): prefix built-in node module importsSzymon Łągiewkabefore adoption4111359
line 9why, one step back9var path = require('node:path');fix(deps): cookie@0.7.0Josh Bukerbefore adoption2027b87
lines 10–43why, one step back10var app = express();1112// log requests13app.use(logger('dev'));1415// express on its own has no notion16// of a "file". The express.static()17// middleware checks for a file matching18// the `req.path` within the directory19// that you pass it. In this case "GET /js/app.js"20// will look for "./public/js/app.js".2122app.use(express.static(path.join(__dirname, 'public')));2324// if you wanted to "prefix" you may use25// the mounting feature of Connect, for example26// "GET /static/js/app.js" instead of "GET /js/app.js".27// The mount-path "/static" is simply removed before28// passing control to the express.static() middleware,29// thus it serves the file correctly by ignoring "/static"30app.use('/static', express.static(path.join(__dirname, 'public')));3132// if for some reason you want to serve files from33// several directories, you can use express.static()34// multiple times! Here we're passing "./public/css",35// this will allow "GET /style.css" instead of "GET /css/style.css":36app.use(express.static(path.join(__dirname, 'public', 'css')));3738app.listen(3000);39console.log('listening on port 3000');40console.log('try:');41console.log(' GET /hello.txt');42console.log(' GET /js/app.js');43console.log(' GET /css/style.css');