1

I am doing a small MEAN app

My javascript inside the html is not loading

below is my express route

app.get("/getuser/:id",userRoute.getUser);

directory structure nodejs/view

****server file****

My Route Function

var getUser=function(req,res)
{
    console.log("hieieieiei");
    res.sendFile(path.join(__dirname + '/view/index.html'));
};

My html file name is index.html and my js file name is test.js . Both are in folder named view .

I have this mentioned in my html . Can someone help

index.html

   <html>
<head lang="en">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
    <script type="text/javascript" src="test.js"></script>
    <meta charset="UTF-8">
    <title> dfdfdfddddddddddddddddddd</title>
</head>
<body>

<button onclick="clicker()"></button>
</body>
</html>

test.js

function clicker()
{
    alert("hi ");
}
3
  • 1
    do you get any error message? Commented Feb 26, 2016 at 12:28
  • 1
    please add more of your code Commented Feb 26, 2016 at 12:28
  • added some more code pls help Commented Feb 26, 2016 at 12:34

1 Answer 1

1

My html file name is index.html and my js file name is test.js . Both are in folder named view .

According to http://expressjs.com/en/starter/static-files.html you need to tell express which directory, or directories, you're serving your static files from using express.static.

So, you could try:

  1. Create a public folder in your project
  2. Move test.js from your views folder to the public folder created in step 1

Then update your configuration code so it includes the following:

app.use(express.static('public'));

Hopefully that'll do the trick.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks and where should my html go .. Can i retain it in the same place ...Thanks a lot again
@BalajiV It might depend: are you using a template engine such as Jade? or just static HTML?
I am using static html and i have java script inside that . I dont want to use any templating engines like jade

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.