Node.js - How to link to local file? - javascript

I have a basic script source link:
// index.html
<script src="/js/jquery.js"></script>
Which doesn't work, despite the file existing. I tried to link to it in the Node.js server but it threw an error that express wasn't defined, yet it is.
//server.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var clientlist = [];
app.get('/', function(req, res) {
res.sendfile('index.html');
app.use(express().static('/js/jquery.js'));
});

Your requires are wrong, and hence express is not defined
Chamnge your first line var app = require('express');
var express = require('express');
var app = express();

Here is my solution.
Note:
You may want to replace '/var/www/nodeserver', with the directory, you are working in!
First of all, don't use res.sendfile(), it is deprecated, use res.sendFile() instead.
Or just serve a complete directory:
Setting all up
index.js
This could be your 'index.js' in '/var/www/nodeserver':
// Setup basic express server
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
// Change 3000 to whatever port, you want to access the site with"http://127.0.0.1:3000"
var port = process.env.PORT || 3000;
server.listen(port, function() {
console.log("Server listening at port "+port);
});
// Routing
var dir = __dirname+'/public'; // Path of the index.js but one dir further (public)
app.use(express.static(dir)); // serve all files in '/var/www/nodeserver/public/'
package.json
And you would need to have a 'package.json', containing this:
{
"name": "nameofyourapplication",
"version": "versionofyourapplication",
"dependencies": {
"express": "^4.10.2",
"socket.io": "^1.3.7"
}
}
Installation
Then install the dependencies defined in the 'package.json', with this command: npm install, while in the directory '/var/www/nodeserver/'.
This will install all the dependencies, locally, so it will create a folder named 'node_modules', in '/var/www/nodeserver'.
Using it
Next you just need to put all the files you want to serve, into the 'public' folder in '/var/www/nodeserver' and run the 'index.js' with node index.js.
The Filetree
Your filetree should then look something like this:
nodeserver
node_modules
express
socket.io
public
js
jquery.js
index.js
package.json
That should do it!

Before your file name put __dirname,'index.html'.
New code will be res.sendfile(__dirname + 'index.html');
And also your first line is wrong it should be var express = require('express');

Related

How to create HTML pages with images with NodeJS [duplicate]

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' })

Why GET method make a request for css file [duplicate]

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' })

Error :Enoent :no such file or directory ,stat 'D:\awesome-testindex.html'

I have a folder named awesome test and it contains index.hml ,node modules and server.js .Here is the server.js file and i am getting this error .
//grab express
var express=require('express');
//create an express App
var app=express();
// create an express route for the home page
// http://localhost:8080/
app.get('/', function(req, res) {
res.sendFile(__dirname + 'index.html');
});
// start the server on port 8080
app.listen(8080);
// send a message
console.log('Server has started!');
Here's where the error is:
res.sendFile(__dirname + 'index.html');
It should be:
res.sendFile(__dirname + '/index.html');
The reason for this is because the index.html is being added upon the directory, which doesn't end with a / by default. You need to add it yourself as shown above.
Hope this helps!
Edit: I tried Node.js before. I think it would be best if you added a "public" folder, with the .js file being above everything. Here's an example:
This was my code for my first Node.js server, as a reference:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const express = require('express');
const app = new express();
app.use(express.static(__dirname + '/public'));
app.listen(3000, () => console.log("Example app listening on port 3000!"))
console.log("http://"+hostname+":"+port)
Note: To use express as shown above, you'll have to open a command line, and type in the following (assuming you have node.js installed correctly):
npm install -g express
Also, to make sure you installed both node.js, do the following:
Node: node -v
Hope everything helps! ^_^

NodeJS Express defining custom routes

This is probably a really basic concept that I'm not understanding but in my NodeJS application I am trying to define a custom route.
my directory structure is as follows
/application
/app.js
/package.json
/node_modules
/public
/routes
/control
/users.js
/views
/control
/users.ejs
Which I am happy with because I want to keep the routes and views in a 1 to 1 relationship because I will eventually end up with something like
/application
/app.js
/package.json
/node_modules
/public
/routes
/control
/users.js
/system.js
/tools
/stock.js
/report.js
/views
/control
/users.ejs
/system.ejs
/tools
/stock.ejs
/report.ejs
So I don't want to end up with a /routes/index.js file with a hideous amount of routing code inside.
It seems to work while my app.js file is as follows
//==============================================================================
// setup
//==============================================================================
var express = require("express");
var path = require("path");
var app = express();
var port = 3000;
var message = null;
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
//==============================================================================
// routes
//==============================================================================
var users = require("./routes/control/users");
app.get("/", users.users);
//==============================================================================
// start server
//==============================================================================
app.listen(port, function() {
message = "Server Started : Port " + port;
console.log(message);
});
Although I can see this is going to end up looking like
//==============================================================================
// setup
//==============================================================================
var express = require("express");
var path = require("path");
var app = express();
var port = 3000;
var message = null;
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, "public")));
//==============================================================================
// routes
//==============================================================================
// control
var users = require("./routes/control/users");
app.get("/", users.users);
var system = require("./routes/control/system");
app.get("/", system.system);
// tools
var stock = require("./routes/tools/stock");
app.get("/", stock.stock);
var report = require("./routes/tools/report");
app.get("/", report.report);
//==============================================================================
// start server
//==============================================================================
app.listen(port, function() {
message = "Server Started : Port " + port;
console.log(message);
});
So I don't really want to have that many requires but doing it like the following doesn't seem to work and I'm not sure why
// control
var control = require("./routes/control");
app.get("/", control.users.users);
app.get("/", control.system.system);
// tools
var tools = require("./routes/tools");
app.get("/", tools.stock.stock);
app.get("/", tools.report.report);
You can use the express Router object to chain your routes. Here's an example
/app.js
var routes = require('./routes/index');
// as noted by Paul in the comments,
// you could use `app.use(routes)` for simplicity
app.use('/', routes);
/routes/index.js
var routes = require('express').Router();
routes.route('/test')
.get(function (req, res) {
res.send(req.originalUrl);
});
routes.use('/control', require('./control/user'));
module.exports = routes;
/routes/control/user.js
var routes = require('express').Router();
routes.route('/test')
.get(function (req, res) {
res.send(req.originalUrl);
});
module.exports = routes;
So for the route defined in index.js, you'll need to send a GET request to /test while in user.js, you will need to send a GET request to /control/test to get a response.
This way all you need to include in the main js file, app.js in your case, is the main routes file, which is index.js under the routes directory. Either way, you will still need to do one require statement for each file that exports a router object.
When you say:
var control = require("./routes/control");
Node will do the followings:
Since you privided a relative path to require, it will search for a routes directory within the current folder and in that directory search for a control.js file. It can't find control.js file so then:
Search for a control.json file. but it also can't find that
file. then:
Search for a control directory. It finds such a directory. opens
it and search for a package.json file to look into its
main property. but it also cant find such a file. then:
Search for a index.js file but also there is no such a file
By default when you give a folder path to require, it does not load .js files inside that folder automatically it looks for package.json file and if it's not there it loads index.js. i. e. It looks for an entry point.
So make an index.js in your control folder:
/routes
/control
/users.js
/system.js
/index.js
index.js:
module.exports = {
users: require('./users');
system: require('./system');
};
Do this also for your tools directory and your last approach should work.
Note that you could consider using express.Router to manage routes.

Cannot GET /about.html express/nodejs

I am trying to use express and body-parser libraries to create a simple node server. It is not supporting the static files as I stated below in the example. What is the mistake which i am making? Kindly help me.
server.js
var express = require("express");
var bodyParser =require("body-parser");
var app = express();
//Here we are configuring express to use body-parser as middle-ware.
app.use(bodyParser.urlencoded({extended:false}));
app.get('/',function(req,res)
{
res.sendfile("index.html");
});
app.listen(3000,function()
{
console.log("Server started on Port 3000");
});
Package.json
{
"name": "blog post",
"version": "0.0.1",
"description": "Package.josn for node server",
"repository": {
"type": "git",
"url": ""
},
"private": true,
"dependencies": {
"body-parser": "^1.14.1",
"express": "^4.13.3"
}
}
I have updated my server.js as per the recommendation.
server.js (updated)
var express = require("express");
var bodyParser =require("body-parser");
var app = express();
//Here we are configuring express to use body-parser as middle-ware.
//app.use(bodyParser.urlencoded({extended:false}));
app.use(express.static(__dirname + '/public'));
app.get('/',function(req,res)
{
res.sendfile("index.html");
});
app.listen(3000,function()
{
console.log("Server started on Port 3000");
});
Worked Version
server.js
var express = require("express");
var bodyParser =require("body-parser");
var app = express();
//Here we are configuring express to use body-parser as middle-ware.
//app.use(bodyParser.urlencoded({extended:false}));
app.use(express.static(__dirname + '/'));
app.get('/',function(req,res)
{
res.sendfile("index.html");
});
app.listen(3000,function()
{
console.log("Server started on Port 3000");
});
You will need to make files public before using them as static files from expressjs server as below -
app.use(express.static('public'));
This serves your public directory as static directory. Put all your static files in public directory and serve.
If you want to serve from multiple directories you can do following -
app.use(express.static('public'));
app.use(express.static('files'));
More info - http://expressjs.com/starter/static-files.html
The only route you show is this:
app.get('/',function(req,res)
That's for the URL: localhost:3000/.
But, the URL in your image is localhost:3000/about.html which you do not have a route for. So, you can either make a route for that particular URL or you can use express.static() to automatically serve files from a particular directory.
Express does not automatically serve ANY files (unlike some other web servers) so any file that you want served must have a route that handles it (either individually or a route that handles many files).

Categories