Reputation: 6632
I have created a Node.js server that worked well when the Javascript was part of the HTML page. I moved the JS to another file and added some images. Now it won't load the images or the JS into the browser. However, the web page renders perfectly when I open the web page directly. This is what my server looks like:
app.get('/',function(req,res){
res.end('Hello World!Go to /map to see the google map');
});
app.get('/map',function(req,res){
var conn;
//images must be sent to the client from the server...
res.sendfile(__dirname+'/client/google_maps.html');
//receiving requests from jQuery
});
I am not using the Express project structure or the Express middleware or Express configuration to do this.
Upvotes: 1
Views: 1624
Reputation: 4656
If that's all your code, I think the problem you met is reasonable. You didn't tell your server how to respond your images and scripts when browser requested. For example in your google_map.html
file you have <script src="myjs.js"></script>
, then your browser will ask your node application to give the content of myjs.js
but your server don't know how to deal with it.
You could try to add code like below to see if it helps.
app.get('/myjs.js', function (req, res) { res.sendfile(__dirname + '/myjs.js'); });
As dimadima said, Express provides a module to handle static files that you can use like
app.use(express.static(__dirname + '/public'));
Upvotes: 3