gpt4 book ai didi

javascript - node.js http 在浏览器中打开静态文本文件

转载 作者:行者123 更新时间:2023-12-03 11:36:20 27 4
gpt4 key购买 nike

如果我在 node.js 中有一个 httpserver,它在端口 3000 上为网站提供服务。

var httpserver = http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});

app.get('/', function(req, res) {
res.render('index.html');
});

我需要访问位于/public/的txt文件

例如,如果我执行 localhost:3000/other_site.html,如果此 other_site.html 位于/public/中,则会打开它,但如果文件是 txt,则不会打开它。

是否有无需安装 http-request、connect 或其他 npm 模块即可解决此问题的解决方案?

问候,

最佳答案

可以用 express 吗?如果是,

app.use(express.static(__dirname + '/public')) 

成功了。

如果没有,请尝试搜索文件:) Using node.js as a simple web server

关于javascript - node.js http 在浏览器中打开静态文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26493744/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com