Skip to content

Commit 99e3418

Browse files
committed
Update static-file-fun examples
1 parent ee632a4 commit 99e3418

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

Chapter_04/static-file-fun/app.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
var app =require('express')();
2-
var path=require('path');
3-
var fs= require('fs');
1+
var express = require("express");
2+
var morgan = require("morgan");
3+
var path = require("path");
44

5-
app.use(function(req,res,next) {
6-
var filePath = path.join(__dirname,"static", req.url);
7-
console.log(filePath);
8-
fs.exists(filePath, function(exists) {
9-
if (exists) {
10-
res.sendFile(filePath);
11-
} else {
12-
next();
13-
}
14-
});
5+
var app = express();
156

7+
app.use(morgan("short"));
8+
9+
var staticPath = path.join(__dirname, "static");
10+
app.use(express.static(staticPath));
11+
12+
app.use(function(req, res) {
13+
res.status(404);
14+
res.send("File not found!");
1615
});
1716

18-
app.listen(3000,function() { console.log('app started');});
17+
app.listen(3000, function() {
18+
console.log("App started on port 3000");
19+
});

Chapter_04/static-file-fun/first-version.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Chapter_04/static-file-fun/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"start": "node app"
55
},
66
"dependencies": {
7-
"express": "^5.0.0-alpha.2"
7+
"express": "^5.0.0-alpha.2",
8+
"morgan": "^1.6.1"
89
}
910
}

0 commit comments

Comments
 (0)