Node.js 节点响应优先级
我的节点应用程序有问题。我在我的节点应用程序中使用express服务器页面。但看起来基本函数:Node.js 节点响应优先级,node.js,Node.js,我的节点应用程序有问题。我在我的节点应用程序中使用express服务器页面。但看起来基本函数: app.use(express.static('public')); app.use(express.static('allure-report')); app.get('/', (req, res) => { //myHtmlCode is read my fs from index.html //res.send(myHtmlCode); //res.render(
app.use(express.static('public'));
app.use(express.static('allure-report'));
app.get('/', (req, res) => {
//myHtmlCode is read my fs from index.html
//res.send(myHtmlCode);
//res.render('index', { html: myHtmlCode })
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.write(myHtmlCode);
});
在所有这些情况下,node将为公用文件夹中的index.html页面提供服务器。如果我从公用文件夹中删除index.html页面,它将从诱惑报告文件夹中服务器index.html
如何告诉node不要提供index.html文件,并在“app.get('/',(req,res)=>{”函数中提供动态内容?
它不适用于发送、渲染或写入
如果index.html文件可用,则节点完成忽略函数或路由“/”
看起来是这样的
app.get('/', function(req, res) {
从未调用。移动
应用程序。获取应用程序上方的。使用s并在请求处理程序中调用next,如下所示:
app.get('/', function(req, res, next) { ...; next(); })
app.use(express.static('public'));
app.use(express.static('allure-report'));
Express按照您定义的顺序处理响应所有app。使用s soExpress.static('public')
或Express.static('allure-report')
将首先处理响应,不会给您的函数运行机会
当您在响应处理程序中调用next()
时,它会将请求传递给行中的下一个处理程序。移动应用程序。在应用程序上方获取。使用s并在请求处理程序中调用next,如下所示:
app.get('/', function(req, res, next) { ...; next(); })
app.use(express.static('public'));
app.use(express.static('allure-report'));
Express按照您定义的顺序处理响应所有app。使用s soExpress.static('public')
或Express.static('allure-report')
将首先处理响应,不会给您的函数运行机会
当您调用next()时
在您的响应处理程序中,它会将请求传递给行中的下一个处理程序。通过重新排序应用程序。使用和应用程序。获取调用。您在代码中写入它们的顺序是它得到匹配的优先级。通过重新排序应用程序。使用和应用程序。获取调用。您在代码中写入它们的顺序是优先顺序它是否匹配。