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

javascript - How to load JS file in html using express server - Stack Overflow

programmeradmin5浏览0评论

I have put my js in a public directory and trying to load the js in my HTML but getting an error.


var express=require('express');
var bodyparser=require('body-parser');
var path=require("path");
var multer=require("multer");
console.log(__dirname);
var app=express();
var upload = multer({ dest: __dirname + '/uploads/' });
// app.set('views', __dirname + '/views');
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended:true}));
app.use(express.static(path.join(__dirname, 'public')));
 app.engine('html', require('ejs').renderFile);
app.get('/',(req,res)=>{
     res.render('some.ejs');
});
app.post('/',upload.single('upl'),(req,res)=>{
    console.log(req.body);
    console.log(req.file);
})
app.listen(3000,()=>{
    console.log("server is up");
})

Here is my HTML code to load the JS:

<script src="/public/web.js"></script>

directory structure

├ public
| └ web.js
├ views
| └ some.ejs
└ server.js

I have put my js in a public directory and trying to load the js in my HTML but getting an error.


var express=require('express');
var bodyparser=require('body-parser');
var path=require("path");
var multer=require("multer");
console.log(__dirname);
var app=express();
var upload = multer({ dest: __dirname + '/uploads/' });
// app.set('views', __dirname + '/views');
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended:true}));
app.use(express.static(path.join(__dirname, 'public')));
 app.engine('html', require('ejs').renderFile);
app.get('/',(req,res)=>{
     res.render('some.ejs');
});
app.post('/',upload.single('upl'),(req,res)=>{
    console.log(req.body);
    console.log(req.file);
})
app.listen(3000,()=>{
    console.log("server is up");
})

Here is my HTML code to load the JS:

<script src="/public/web.js"></script>

directory structure

├ public
| └ web.js
├ views
| └ some.ejs
└ server.js
Share Improve this question edited Oct 27, 2018 at 11:35 Idin Khayami 2521 silver badge8 bronze badges asked Oct 27, 2018 at 6:51 RatanRatan 1294 silver badges8 bronze badges 2
  • 2 Please include the error message. – Molda Commented Oct 27, 2018 at 6:56
  • 1 Also after you add express.static as mention in bellow answer you need to change your path like so <script src="/web.js"></script> the /public part is not needed. – Molda Commented Oct 27, 2018 at 6:59
Add a ment  | 

2 Answers 2

Reset to default 3

To serve a file from the server to the client, you must declare the directory as a static.

Using express you can do this using,

app.use(express.static('PublicDirectoryPath'))

To fetch the files under the public directory,

<script src="FilePathUnderPublicDirectory"></script>

Updated Code:

Now your server.js file should be

var express=require('express');
var bodyparser=require('body-parser');
var path=require("path");
var multer=require("multer");
console.log(__dirname);
var app=express();
app.use(express.static('public'));
var upload = multer({ dest: __dirname + '/uploads/' });
// app.set('views', __dirname + '/views');
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended:true}));
app.use(express.static(path.join(__dirname, 'public')));
 app.engine('html', require('ejs').renderFile);
app.get('/',(req,res)=>{
     res.render('some.ejs');
});
app.post('/',upload.single('upl'),(req,res)=>{
    console.log(req.body);
    console.log(req.file);
})
app.listen(3000,()=>{
    console.log("server is up");
})

Notice that I declare the public directory as a static directory at line 7.

Since your web3.js is directly under the public directory, in your front end, use

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

For more, please check the doc.

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

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

now you can serve static files like js, css and images.

发布评论

评论列表(0)

  1. 暂无评论