Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

javascript - res.sendFile(path.join(__dirname, '../public', 'mapv02.html'));

mapv02.html is sending rigthly,but the '.js' in mapv01.html can not be found!

//routes/restapi.js

res.sendFile(path.join(__dirname, '../public', 'mapv02.html'));

//public/mapv02.html

<script type="text/javascript" src="js/jquery-2.1.4.js"></script>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To serve static files such as images, CSS files, and JavaScript files, use the express.static built-in middleware function in Express.

Pass the name of the directory that contains the static assets to the express.static middleware function to start serving the files directly.

Example:- Use the following code to serve images, CSS files, and JavaScript files in a directory named public:

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

Now, you can load the files that are in the public directory:

http://localhost:3000/images/kitten.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js
http://localhost:3000/images/bg.png

After this your script will be loaded successfully.

<script type="text/javascript" src="js/jquery-2.1.4.js"></script>

For more reference see this link - http://expressjs.com/en/starter/static-files.html

Hope this will help to solve your query !!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...