I was trying to implement the following code and got the TypeError error when I ran it.
app.js
var app = module.exports = require('express').createServer();
var io = require('socket.io').listen(app);
var path = require('path');
app.listen(3000);
app.get('/',function(req,res){
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function(socket){
socket.emit('welcome', {text: 'Welcome!!!'});
});
Error Output:
TypeError: Path must be a string. Received null
at assertPath (path.js:8:11)
at posix.join (path.js:479:5)
at exports.send (/Users/rluo/Desktop/learn/learnNode/socket.io_epxress/node_modules/express/node_modules/connect/lib/middleware/static.js:129:20)
at ServerResponse.res.sendfile (/Users/rluo/Desktop/learn/learnNode/socket.io_epxress/node_modules/express/lib/response.js:186:3)
at /Users/rluo/Desktop/learn/learnNode/socket.io_epxress/app.js:8:6
at callbacks (/Users/rluo/Desktop/learn/learnNode/socket.io_epxress/node_modules/express/lib/router/index.js:272:11)
at param (/Users/rluo/Desktop/learn/learnNode/socket.io_epxress/node_modules/express/lib/router/index.js:246:11)
at pass (/Users/rluo/Desktop/learn/learnNode/socket.io_epxress/node_modules/express/lib/router/index.js:253:5)
at Router._dispatch (/Users/rluo/Desktop/learn/learnNode/socket.io_epxress/node_modules/express/lib/router/index.js:280:5)
at Object.Router.middleware [as handle] (/Users/rluo/Desktop/learn/learnNode/socket.io_epxress/node_modules/express/lib/router/index.js:45:10)
package.json:
{
"name":"socketio_express-example",
"version":"0.0.1",
"private":true,
"dependencies":{
"socket.io":"0.8.7",
"express":"2.5.4"
}
}
Thanks in advance.
The error is pretty clear, you need to specify an absolute (instead of relative) path
Examples:
// assuming index.html is in the same directory as this script
res.sendFile(__dirname + '/index.html');
You do not need path at all
Global Objects
__dirname
Added in: v0.1.27
The name of the directory that the currently executing script resides in.for more detail https://nodejs.org/docs/latest/api/globals.html
check this thread TypeError: Path must be a string
Creating socket
var app = require('express')();
var http = require('http').Server(app);
Express initializes app to be a function handler that you can supply to an HTTP server (as seen in line 2).
socket.io
__dirname vs path
Before you use res.sendFile() you need to set static files directory like below.
var express = require('express');
var app = express();
app.use(express.static(path.join(__dirname+'your index or static files location'));
app.get('/',function(req,res){
res.sendFile(__dirname+'index.html');
});
Please use the 'path' module that you have required. Try this:
app.get('/',function(req,res){
res.sendfile(path.join(__dirname, '/index.html'));
});
Related
I want to serve index.html and /media subdirectory as static files. The index file should be served both at /index.html and / URLs.
I have
web_server.use("/media", express.static(__dirname + '/media'));
web_server.use("/", express.static(__dirname));
but the second line apparently serves the entire __dirname, including all files in it (not just index.html and media), which I don't want.
I also tried
web_server.use("/", express.static(__dirname + '/index.html'));
but accessing the base URL / then leads to a request to web_server/index.html/index.html (double index.html component), which of course fails.
Any ideas?
By the way, I could find absolutely no documentation in Express on this topic (static() + its params)... frustrating. A doc link is also welcome.
If you have this setup
/app
/public/index.html
/media
Then this should get what you wanted
var express = require('express');
//var server = express.createServer();
// express.createServer() is deprecated.
var server = express(); // better instead
server.configure(function(){
server.use('/media', express.static(__dirname + '/media'));
server.use(express.static(__dirname + '/public'));
});
server.listen(3000);
The trick is leaving this line as last fallback
server.use(express.static(__dirname + '/public'));
As for documentation, since Express uses connect middleware, I found it easier to just look at the connect source code directly.
For example this line shows that index.html is supported
https://github.com/senchalabs/connect/blob/2.3.3/lib/middleware/static.js#L140
In the newest version of express the "createServer" is deprecated. This example works for me:
var express = require('express');
var app = express();
var path = require('path');
//app.use(express.static(__dirname)); // Current directory is root
app.use(express.static(path.join(__dirname, 'public'))); // "public" off of current is root
app.listen(80);
console.log('Listening on port 80');
express.static() expects the first parameter to be a path of a directory, not a filename. I would suggest creating another subdirectory to contain your index.html and use that.
Serving static files in Express documentation, or more detailed serve-static documentation, including the default behavior of serving index.html:
By default this module will send “index.html” files in response to a request on a directory. To disable this set false or to supply a new index pass a string or an array in preferred order.
res.sendFile & express.static both will work for this
var express = require('express');
var app = express();
var path = require('path');
var public = path.join(__dirname, 'public');
// viewed at http://localhost:8080
app.get('/', function(req, res) {
res.sendFile(path.join(public, 'index.html'));
});
app.use('/', express.static(public));
app.listen(8080);
Where public is the folder in which the client side code is
As suggested by #ATOzTOA and clarified by #Vozzie, path.join takes the paths to join as arguments, the + passes a single argument to path.
const path = require('path');
const express = require('express');
const app = new express();
app.use(express.static('/media'));
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, 'media/page/', 'index.html'));
});
app.listen(4000, () => {
console.log('App listening on port 4000')
})
If you have a complicated folder structure, such as
- application
- assets
- images
- profile.jpg
- web
- server
- index.js
If you want to serve assets/images from index.js
app.use('/images', express.static(path.join(__dirname, '..', 'assets', 'images')))
To view from your browser
http://localhost:4000/images/profile.jpg
If you need more clarification comment, I'll elaborate.
use below inside your app.js
app.use(express.static('folderName'));
(folderName is folder which has files) - remember these assets are accessed direct through server path (i.e. http://localhost:3000/abc.png (where as abc.png is inside folderName folder)
npm install serve-index
var express = require('express')
var serveIndex = require('serve-index')
var path = require('path')
var serveStatic = require('serve-static')
var app = express()
var port = process.env.PORT || 3000;
/**for files */
app.use(serveStatic(path.join(__dirname, 'public')));
/**for directory */
app.use('/', express.static('public'), serveIndex('public', {'icons': true}))
// Listen
app.listen(port, function () {
console.log('listening on port:',+ port );
})
I would add something that is on the express docs, and it's sometimes misread in tutorials or others.
app.use(mountpoint, middleware)
mountpoint is a virtual path, it is not in the filesystem (even if it actually exists). The mountpoint for the middleware is the app.js folder.
Now
app.use('/static', express.static('public')`
will send files with path /static/hell/meow/a.js to /public/hell/meow/a.js
This is the error in my case when I provide links to HTML files.
before:
<link rel="stylesheet" href="/public/style.css">
After:
<link rel="stylesheet" href="/style.css">
I just removed the static directory path from the link and the error is gone. This solves my error one thing more don't forget to put this line where you are creating the server.
var path = require('path');
app.use(serveStatic(path.join(__dirname, 'public')));
You can achieve this by just passing the second parameter express.static() method to specify index files in the folder
const express = require('express');
const app = new express();
app.use(express.static('/media'), { index: 'whatever.html' })
I want to serve index.html and /media subdirectory as static files. The index file should be served both at /index.html and / URLs.
I have
web_server.use("/media", express.static(__dirname + '/media'));
web_server.use("/", express.static(__dirname));
but the second line apparently serves the entire __dirname, including all files in it (not just index.html and media), which I don't want.
I also tried
web_server.use("/", express.static(__dirname + '/index.html'));
but accessing the base URL / then leads to a request to web_server/index.html/index.html (double index.html component), which of course fails.
Any ideas?
By the way, I could find absolutely no documentation in Express on this topic (static() + its params)... frustrating. A doc link is also welcome.
If you have this setup
/app
/public/index.html
/media
Then this should get what you wanted
var express = require('express');
//var server = express.createServer();
// express.createServer() is deprecated.
var server = express(); // better instead
server.configure(function(){
server.use('/media', express.static(__dirname + '/media'));
server.use(express.static(__dirname + '/public'));
});
server.listen(3000);
The trick is leaving this line as last fallback
server.use(express.static(__dirname + '/public'));
As for documentation, since Express uses connect middleware, I found it easier to just look at the connect source code directly.
For example this line shows that index.html is supported
https://github.com/senchalabs/connect/blob/2.3.3/lib/middleware/static.js#L140
In the newest version of express the "createServer" is deprecated. This example works for me:
var express = require('express');
var app = express();
var path = require('path');
//app.use(express.static(__dirname)); // Current directory is root
app.use(express.static(path.join(__dirname, 'public'))); // "public" off of current is root
app.listen(80);
console.log('Listening on port 80');
express.static() expects the first parameter to be a path of a directory, not a filename. I would suggest creating another subdirectory to contain your index.html and use that.
Serving static files in Express documentation, or more detailed serve-static documentation, including the default behavior of serving index.html:
By default this module will send “index.html” files in response to a request on a directory. To disable this set false or to supply a new index pass a string or an array in preferred order.
res.sendFile & express.static both will work for this
var express = require('express');
var app = express();
var path = require('path');
var public = path.join(__dirname, 'public');
// viewed at http://localhost:8080
app.get('/', function(req, res) {
res.sendFile(path.join(public, 'index.html'));
});
app.use('/', express.static(public));
app.listen(8080);
Where public is the folder in which the client side code is
As suggested by #ATOzTOA and clarified by #Vozzie, path.join takes the paths to join as arguments, the + passes a single argument to path.
const path = require('path');
const express = require('express');
const app = new express();
app.use(express.static('/media'));
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, 'media/page/', 'index.html'));
});
app.listen(4000, () => {
console.log('App listening on port 4000')
})
If you have a complicated folder structure, such as
- application
- assets
- images
- profile.jpg
- web
- server
- index.js
If you want to serve assets/images from index.js
app.use('/images', express.static(path.join(__dirname, '..', 'assets', 'images')))
To view from your browser
http://localhost:4000/images/profile.jpg
If you need more clarification comment, I'll elaborate.
use below inside your app.js
app.use(express.static('folderName'));
(folderName is folder which has files) - remember these assets are accessed direct through server path (i.e. http://localhost:3000/abc.png (where as abc.png is inside folderName folder)
npm install serve-index
var express = require('express')
var serveIndex = require('serve-index')
var path = require('path')
var serveStatic = require('serve-static')
var app = express()
var port = process.env.PORT || 3000;
/**for files */
app.use(serveStatic(path.join(__dirname, 'public')));
/**for directory */
app.use('/', express.static('public'), serveIndex('public', {'icons': true}))
// Listen
app.listen(port, function () {
console.log('listening on port:',+ port );
})
I would add something that is on the express docs, and it's sometimes misread in tutorials or others.
app.use(mountpoint, middleware)
mountpoint is a virtual path, it is not in the filesystem (even if it actually exists). The mountpoint for the middleware is the app.js folder.
Now
app.use('/static', express.static('public')`
will send files with path /static/hell/meow/a.js to /public/hell/meow/a.js
This is the error in my case when I provide links to HTML files.
before:
<link rel="stylesheet" href="/public/style.css">
After:
<link rel="stylesheet" href="/style.css">
I just removed the static directory path from the link and the error is gone. This solves my error one thing more don't forget to put this line where you are creating the server.
var path = require('path');
app.use(serveStatic(path.join(__dirname, 'public')));
You can achieve this by just passing the second parameter express.static() method to specify index files in the folder
const express = require('express');
const app = new express();
app.use(express.static('/media'), { index: 'whatever.html' })
I hope this question isn't too ridiculous.
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req,res){
res.sendFile('index.html');
});
http.listen(3000,function(){
console.log('/','listening on *:3000');
});
I am running linux mint, and whenever I try to run this locally I get the following error:
Error: ENOENT, stat 'index.html'
at Error (native)
I believe it has something to do with the directory. The index.html file is in the same folder as the index.js file. I've searched around and couldn't find this exact error in this case so am kind of confused. Am I putting in the wrong directory for the
app.get('/', function(req,res){
res.sendFile('index.html');
});
You need to specify exactly where your index.html file is located. Try using the following code snippet
var app = require('express')();
var http = require('http').Server(app);
var path = require('path');
app.get('/', function(req,res){
res.sendFile(path.join(__dirname, 'index.html'));
});
http.listen(3000,function(){
console.log('/','listening on *:3000');
});
I have some problems when running my Node-Server on Remote via an Service.
I get the following Error:
Error: Failed to lookup view "index" in views directory
"/home/naoufal/etc/run-nodeServer/views" at EventEmitter.render
(/var/www/virtual/naoufal/html/node_modules/express/lib/application.js:579:17)
He looks for my Templatefolder in the path, where the Servicescript is started, namely (/home/naoufal/etc/run-nodeServer/...)
My Node-Application is on (~/html/...).
var express = require('express');
var app = express();
var ECT = require('ect');
var ectRenderer = ECT({watch: true, root: __dirname + '/views',ext: '.ect'});
app.set('view engine','ect');
app.engine('ect',ectRenderer.render);
app.get('/',function(req, res){
res.render('index');
});
app.listen(68000);
I tried instead of ('__dirname + '/views')
This '/home/naoufal/html/views' but the same error occurs...
Any suggestions?
ok I found the solution:
app.get('/',function(req, res){
res.render('index');
});
Here he did not use the right Path, from the Renderer. I don't know why? Instead it takes the path of the executing script...
app.get('/',function(req, res){
res.render(__dirname + '/views/index');
});
solved the problem anyway.
here is my code
var express=require("express");
var app=express();
var port=8181;
app.use(express.static(__dirname));
app.listen(port);
it is serving static file properly
I want to log when a file with an extension .xls is being requested
how can i achieve it ?
The path core module gives you the tools to deal with this. So, just put this logic in a middleware before your static middleware, like:
var express = require("express");
var path = require("path");
var app = express();
var port = 8181;
app.use(function (req, res, next) {
var filename = path.basename(req.url);
var extension = path.extname(filename);
if (extension === '.css')
console.log("The file " + filename + " was requested.");
next();
});
app.use(express.static(__dirname));
app.listen(port);
You want to log what serve-static (express.static) gives in response. There're several ways to do this.
Method Ⅰ A middleware for manual checks.
You may put (app.use) a middleware that logs a request if it's for express.static, before express.static. Rodrigo Medeiros' answer does this. But this way, you have to rewrite the code for checks when the options for the serve-static middleware changes, which might be a maintaining issue.
Method Ⅱ Hooking into express.static; leaking info out of it.
Well, express.static knows what files it gives best. It just, unfortunately, does not let us know and log it. But there's a hack for this: the setHeaders option, which is a callback function supposedly used to set custom response headers. It's called when express.static makes a response and gets enough information to log what you want to.
const express = require("express");
const path = require("path");
const app = express();
const asset_dir_path = "assets/";
app.use(express.static(asset_dir_path, {
index: false,
setHeaders: (response, file_path, file_stats) => {
// This function is called when “serve-static” makes a response.
// Note that `file_path` is an absolute path.
// Logging work
const relative_path = path.join(asset_dir_path, path.relative(asset_dir_path, file_path));
console.info(`#${Date.now()}`, "GAVE\t\t", relative_path);
}
}));
Just do
var express=require("express");
var app=express();
var port=8181;
app.use(function(req, res, next) {
// check for .xls extension
console.log(req.originalUrl);
next();
}, express.static(__dirname));
app.listen(port);