Reputation: 61
I am following an example of using express's static middleware in the express in action book by Evan Hann.
Here is the code I wrote
var express = require('express');
var path = require('path');
var http = require('http');
var app = express();
// sets up the public path, using Node's path module
var publicPath = path.resolve(__dirname, "public");
app.use(express.static(publicPath));
app.use(function (req, res) {
res.writeHead(200, { "Content-Type": "text/plain"});
res.end("Look like you didn't find a static file.")
});
http.createServer(app).listen(3000);
Normally the application should serve me whatever is in the public directory. I put an image in the public folder (Twitter_32.png). But nothing is happening when I navigate to http://localhost:3000/.
What am I missing?
Here is the content of package.json:
{
"name": "hello-world",
"version": "1.0.0",
"description": "hello world example using expressjs",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"hello",
"world",
"express"
],
"author": "Sandro Agboka",
"license": "ISC",
"dependencies": {
"eslint": "^4.19.1",
"express": "^4.16.3",
"morgan": "^1.9.0",
"tslint": "^5.9.1",
"typescript": "^2.8.3"
}
}
Here is my project directory structure
drwxr-xr-x 8 macbook staff 272 May 4 14:59 .
drwxr-xr-x 4 macbook staff 136 May 3 16:04 ..
-rw-r--r-- 1 macbook staff 1155 May 3 17:48 app-old.js
-rw-r--r-- 1 macbook staff 440 May 4 15:02 app.js
drwxr-xr-x 191 macbook staff 6494 May 3 17:40 node_modules
-rw-r--r-- 1 macbook staff 53133 May 3 17:40 package-lock.json
-rw-r--r-- 1 macbook staff 465 May 3 17:40 package.json
drwxr-xr-x 3 macbook staff 102 May 4 14:59 public
Upvotes: 0
Views: 38
Reputation: 1255
You need to navigate to the full resource path : http://localhost:3000/Twitter_32.png
Upvotes: 1