带有 Express 错误的 NodeJS:无法获取/ [英] NodeJS w/Express Error: Cannot GET /

查看:13
本文介绍了带有 Express 错误的 NodeJS:无法获取/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我所拥有的,文件名default.htm"实际上存在并在使用 NodeJS 执行 readFile 时加载.

This is what i have, the filename "default.htm" actually exists and loads when doing a readFile with NodeJS.

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

app.use(express.static(__dirname + '/default.htm'));

app.listen(process.env.PORT);

错误(在浏览器中):

Cannot GET /

推荐答案

您通常希望呈现这样的模板:

You typically want to render templates like this:

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

但是,您也可以提供静态内容 - 为此,请使用:

However you can also deliver static content - to do so use:

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

现在,您项目的 /public 目录中的所有内容都将作为站点根目录下的静态内容进行交付,例如如果您将 default.htm 放在公共文件夹中,如果可以通过访问 /default.htm

Now everything in the /public directory of your project will be delivered as static content at the root of your site e.g. if you place default.htm in the public folder if will be available by visiting /default.htm

查看 express API连接静态中间件 文档了解更多信息.

Take a look through the express API and Connect Static middleware docs for more info.

这篇关于带有 Express 错误的 NodeJS:无法获取/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆