JSt
JSt

Reputation: 819

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

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 ..

<!doctype html>
<html>
    <head>
        <title>Spektrum</title>
        <script src="test.js"></script>
        <meta name = "viewport" content = "initial-scale = 1, user-scalable = no">
        <style>
            canvas{     

            }
        </style>
    </head>
    <body>    
        <!-- some stuff --> 
        </body> 
</html>  

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

Upvotes: 0

Views: 281

Answers (1)

dark_ruby
dark_ruby

Reputation: 7866

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

Upvotes: 3

Related Questions