【问题标题】:Cannot Get/ in node.js无法在 node.js 中获取/
【发布时间】:2019-03-21 19:54:53
【问题描述】:

我正在关注这个http://www.expertphp.in/article/user-login-and-registration-using-nodejs-and-mysql-with-example链接

我得到一个 cannot GET/ 错误,我已经尝试了所有的 stackoverflow 答案,但没有得到解决。

var express=require("express");
var bodyParser=require('body-parser');
var app = express();
var authenticateController=require('./controllers/authenticate-controller');
var registerController=require('./controllers/register-controller');
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
/* route to handle login and registration */
app.get('/', function (req, res) {
    res.render('index', {});
  });
app.post('/api/register',registerController.register);
app.post('/api/authenticate',authenticateController.authenticate);
app.listen(8012);

这是我当前的代码

【问题讨论】:

  • 路径/被处理,返回一个空对象{}。如果您真的想收到错误消息,请尝试删除显示app.get(... 的代码部分

标签: node.js express authentication get body-parser


【解决方案1】:

据我了解,您正在尝试在路径 / 处发送 index.html。这很好,但不要为此使用render。只需发送index.html

// replace this
app.get('/', function (req, res) {
  res.render('index', {});
});
// by this
app.get('/', (req, res) => res.sendFile('full/path/to/index.html'))

有用的链接:

【讨论】:

    【解决方案2】:

    你有两种方法:

    1) 读取index.html 内容并在根网址提供:

    const fs = require('fs');
    
    const indexFileContent = fs.readFileSync('path/to/index.html'); // caching file content to variable, to avoid re-reading it
    app.get('/', (req, res) => {
      res.send(indexFileContent);
    });
    

    2) 也将ejs 渲染器定义为.html 文件渲染器:

    const path = require('path');
    const bodyParser = require('body-parser');
    const express = require("express");
    const app = express();
    
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine','ejs'); // make sure You've installed ejs:  npm i --save ejs
    app.engine('ejs', require('ejs').renderFile);
    app.engine('html', require('ejs').renderFile); // defining html renderer engine
    
    app.use(bodyParser.urlencoded({extended:true}));
    app.use(bodyParser.json());
    
    
    app.get('/', (req, res) => res.render('index'));
    
    
    // API controllers
    const AuthController = require('./controllers/authenticate-controller');
    const RegistrationController = require('./controllers/register-controller');
    
    // API endpoints
    app.post('/api/authenticate', AuthController.authenticate);
    app.post('/api/register', RegistrationController.register);
    
    app.listen(8012);
    

    利弊:

    1-st 示例将简单地读取 index.html 一次variable 并提供该内容,但需要重新启动应用才能重新读取该文件。

    2-nd 示例使用 views 文件夹中的 .html 文件作为 ejs 文件,这给了 机会 来传递 变量 到 html 文件,它更好 比简单地发送文件,+ 您可以使用 <% include partials/header.ejs %>

    将一个 html 包含在另一个 html 中

    【讨论】:

    • 第二个选项很棒,但第一个有几个缺陷,首先是它在 NodeJS 中使用同步编程,而 express 有 a built-in function to serve files
    • @NinoFiliu 我并不是说 Express 没有用于发送文件的内置功能。我是说您可以将文件内容缓存到文件中,并且每次请求到达根路由时都不要使用磁盘驱动器进行 i/o。你也知道 express 有静态文件服务器中间件,而 index.html 是这种情况下的静态文件。事实上,作为使用 NGinx 提供静态文件的人,我不喜欢 .sendFilestatic 中间件。所以这就是我明智地完成它并缓存文件的原因。当你 DDOS / 路由并比较 sendFile 与缓存内容服务时,它会有所不同
    • 哦,那好吧!我没这么看,谢谢你的提示:)
    猜你喜欢
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多