admin管理员组文章数量:1406710
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'));
});
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'));
});
Share
Improve this question
asked May 3, 2016 at 19:47
Terry AndersonTerry Anderson
1091 gold badge2 silver badges10 bronze badges
5
-
have you set your public folder and added js there? e.g.
app.use(express.static(__dirname + '/public'));
and make a folder js with jquery.js in it – Jakob Commented May 3, 2016 at 20:00 - Same thing, says express is not defined. – Terry Anderson Commented May 3, 2016 at 20:09
-
1
you are not defining
express
anywhere – Soren Commented May 3, 2016 at 20:21 - I require it, and then use what the value of the definition would be, as you said I need to do: var express = express(); – Terry Anderson Commented May 3, 2016 at 20:29
-
@TerryAnderson -- no
var express = express();
is wrong, and not what I said in my answer. – Soren Commented May 4, 2016 at 1:44
3 Answers
Reset to default 2Your 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 plete 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 mand: 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
- js
- index.js
- package.json
- node_modules
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');
本文标签: javascriptNodejsHow to link to local fileStack Overflow
版权声明:本文标题:javascript - Node.js - How to link to local file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744905627a2631602.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论