NodeJS w / Express错误:无法GET / [英] NodeJS w/Express Error: Cannot GET /

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

问题描述

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

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.

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

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