最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Serve client side JavaScript in Express.js app - Stack Overflow

programmeradmin2浏览0评论

I have recently started to build my first Node.js app using Express. I have used the latest Express generator to create a skeleton app and I have successfully create several different layouts using Jade and CSS - this all works fine.

However, I cannot seem to get my client side JS to work. In my public folder I have a separate front end JS file and the only thing it contains is an alert (just for testing purposes). I can successfully navigate to the file in the browser and I am not receiving any console errors, however the alert never fires - What am I doing wrong?

I have recently started to build my first Node.js app using Express. I have used the latest Express generator to create a skeleton app and I have successfully create several different layouts using Jade and CSS - this all works fine.

However, I cannot seem to get my client side JS to work. In my public folder I have a separate front end JS file and the only thing it contains is an alert (just for testing purposes). I can successfully navigate to the file in the browser and I am not receiving any console errors, however the alert never fires - What am I doing wrong?

Share Improve this question edited Jul 28, 2016 at 13:55 syscll asked Aug 17, 2015 at 20:02 syscllsyscll 1,0312 gold badges11 silver badges27 bronze badges 4
  • If you can navigate to the file in the browser, and you're linking to it in the same way in your html, it should be working. We'd need to see both the js, and the html that includes it as well as the express code that serves the static files and the folder structure to further diagnose this. – Kevin B Commented Aug 17, 2015 at 20:05
  • Did you import the js in the html file you are serving? CAn you check if your JS file is being loaded? – albertosh Commented Aug 17, 2015 at 20:06
  • 2 Well, I feel like an amateur. I was using href to reference my script instead of src. It all works fine now. – syscll Commented Aug 17, 2015 at 20:10
  • 2 I would answer my own question and mark it as the answer, good job :) – beautifulcoder Commented Aug 17, 2015 at 20:49
Add a ment  | 

1 Answer 1

Reset to default 3

In order to solve this problem I had to change the link reference to a script reference in my HTML. I also had to ensure I served static files in Express.

http://expressjs./starter/static-files.html

发布评论

评论列表(0)

  1. 暂无评论