Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Add a serverside stored javascript file to html (node.js)

Add a serverside stored javascript file to html (node.js)

Problem

I'm having a small problem with my project. I'm using node and express for a little webpage .. it is rendering the Html page, but the Javascript file (test.js) is not sent to the user ..



    
        Spektrum

Node.js file:

var http = require('http'),  
    fs = require('fs'),
    express = require('express'), 
    app     = express(); 
    app.use(express.bodyParser());
    app.set('view engine', 'ejs');
    app.engine('html', require('ejs').renderFile);

// ..... 
    app.listen(8080, function() {
  console.log('Server running at http://127.0.0.1:8080/');
}); 

Any idea how to send the javascript file with the html file to the user?

Greetings, JS

Problem courtesy of: JSt

Solution

you need to use static middleware to serve file (test.js needs to exist)

example:

// GET /javascripts/jquery.js
// GET /style.css
// GET /favicon.ico
app.use(express.static(__dirname + '/public'));
Solution courtesy of: dark_ruby

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Add a serverside stored javascript file to html (node.js)

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×