我对 Express Node.js 完全陌生。我的文件夹结构如下:
/Express
--server.js
--index.html
--home.html
/css/
/js/
/images
/fonts
在index.html
中,我正在访问一些javascript(js文件夹)
、images(图像文件夹)
、fonts(fonts文件夹)
。我还从 index.html
页面链接 home.html
。我还需要阻止直接访问 home.html
。
我用express-ws
编写了一个server.js
,如下所示:
var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);
var path = require('path');
//accessing index.html
app.get('/', function(req, res){
res.sendFile(path.join(__dirname+'/index.html'));
});
//How can i acess all the files inside the JS , CSS , Image and Font folder here ???
//Also How can i link home.html from Index.html File and block home.html accessing publicly
app.ws('/', function(ws, req) {
ws.on('message', function(msg) {
console.log(msg);
});
console.log('socket', req.testing);
});
app.listen(8888);
有什么建议我如何使用 server.js
从 index.html
访问 JS 、 CSS 、 Image 和 Font 文件夹中的所有文件?还可以使用直接访问 block 从 index.html
链接 home.html
。
最佳答案
您应该为 public
等静态内容或其他内容创建一个单独的文件夹,然后使用 express.static()
提供静态内容。所以这将是您更新的目录结构
/Express
--server.js
--index.html
--home.html
--/public/ (directory)
--/css/ (directory)
--/js/ (directory)
--/images/ (directory)
--/fonts/ (directory)
您的更新代码将是
var express = require('express');
var app = express();
var expressWs = require('express-ws')(app);
var path = require('path');
//for js, css, images and fonts
app.use(express.static(path.join(__dirname, 'public')));
//accessing index.html
app.get('/', function(req, res){
res.sendFile(path.join(__dirname, 'index.html'));
});
app.ws('/', function(ws, req) {
ws.on('message', function(msg) {
console.log(msg);
});
console.log('socket', req.testing);
});
app.listen(8888);
关于html - 如何在Express Nodejs中访问路径权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39740190/