2

I am new to nodejs and expressjs so Im trying to build simple web app to grasp both frameworks. Here I have built a project with the following architecture:

js
   test.js
views
   index.html
server.js

my server file looks like this:

var fs = require("fs");
var host = "127.0.0.1";
var port = 1337;
var express = require("express");
var ejs =  require("ejs");

var server = express();
server.use(server.router);
server.use(express(__dirname)); 
server.set('view engine','html');
server.engine('html', require('ejs').renderFile);

server.get("*", function(request, response){ 
    response.render('index.html');
});

server.listen(port, host);

and my index file like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Starter Template for Bootstrap</title>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
    <script language="javascript"  src="/js/test.js" type="text/javascript"></script>
</head>

<body ng-app>
    <div class="container">
        <div class="starter-template">
           some code
        </div>

    </div><!-- /.container -->
</body>
</html>

My index.html file loads properly but I cannot get the test.js file to load. How can I fix this?

8
  • Do you get any errors in the console? Commented Feb 26, 2014 at 12:51
  • in the terminal I dont get any error but on the client side I get this: Uncaught SyntaxError: Unexpected token < bootstrap.min.js:1 Uncaught SyntaxError: Unexpected token < test.js:1 Commented Feb 26, 2014 at 12:54
  • 1
    @ArnaudDrizard if console reports you an error in test.js it mean that test.js have been loaded at least :) Commented Feb 26, 2014 at 12:58
  • 1
    Is your test.js file validated? and where is bootstrap.min.js coming from? Commented Feb 26, 2014 at 12:59
  • what I dont get is that I only put console.log('test'); in test.js and nothing gets printed Commented Feb 26, 2014 at 12:59

5 Answers 5

2

Use express.static built-in middleware function in Express.

Add this line after server.engine. This mechanism should allow static files to be served e.g. images, javascripts, css

app.use(express.static(__dirname + '/js'));

Now you can load:

http://localhost:1337/js/test.js
Sign up to request clarification or add additional context in comments.

Comments

0

change render into sendfile and move your js folder to public folder then add this middleware server.use(express(__dirname+'/public'));

Comments

0

JS files are rendered as static files. Ref: https://expressjs.com/en/starter/static-files.html First create a folder "static" and keep .js files in it. Now, correct the script tags of these .js files in .ejs file.

Comments

0

in the nodejs side :

var path    = require("path");
app.use(express.static(path.join(__dirname + '/js')));//middleware

in the HTML page :

<script src="./my_script.js"></script>

Comments

0

As this files is static, you should add the entire root directory to 'app'.

Like this:

app.use(express.static(path.join(__dirname, '/')));

So that you can quote all of your files by relative path.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.