簡體   English   中英

節點JS基本路由器被覆蓋

[英]Node JS Base Router being overwritten

我的server.js文件如下所示:

var fs = require('fs');
var express = require('express');
var app = express();

// event listener
var events = require('events');
var eventEmitter = new events.EventEmitter();

var port = process.env.PORT || 3000;

// set the view engine to ejs
app.set('view engine', 'ejs');

// make express look in the public directory for assets (css/js/img)
app.use(express.static(__dirname + '/public'));

// set the home page route
app.get('/', function (req, res) {
  console.log('home page loaded');
});

app.listen(port, function () {
  console.log('Server listening on port ' + port);
});

我是node / express的新手,但希望得到一些說明。 我以為app.get('/', ...將處理默認功能。但是,當我運行node server.js時,它不是從消息中記錄日志到控制台,而是從/public加載index.html

什么是覆蓋路由功能? 如果有幫助,則快速版本為4.11.1。

由於serve-static模塊是您正在使用的中間件,因此您遇到了此問題: express.static()

從其文檔中注意:

默認情況下,此模塊將發送“ index.html”文件以響應目錄請求。 要禁用此設置false或提供新的索引,請按首選順序傳遞字符串或數組。

即替換: app.use(express.static(__dirname + '/public'));

使用: app.use(express.static(__dirname + '/public',{'index':false}));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM