简体   繁体   English

节点JS无法获取/

[英]Node JS Cannot Get /

I am following this Node JS tutorial, but I have encountered a problem where I get the response Cannot GET / instead of Hello . 我正在关注 Node JS教程,但是遇到一个问题,我得到的响应Cannot GET /而不是Hello Here is the structure for my code: 这是我的代码的结构:

myapp/
├── app
│   └── routes
│       ├── index.js
│       └── note_routes.js
├── conf
│   ├── httpd-app.conf
│   └── httpd-prefix.conf
├── htdocs
├── node_modules
│   ├── ...
├── package.json
├── package-lock.json
└── server.js

server.js looks like this: server.js看起来像这样:

const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const bodyParser = require('body-parser');

const app = express();

const port = 3000;

require('./app/routes')(app, {});
app.listen(port, () => {
  console.log('We are live on ' + port);
});

index.js is this: index.js是这样的:

const noteRoutes = require('./note_routes');
module.exports = function(app, db) {
  noteRoutes(app, db);
  // Other route groups could go here, in the future
};

And note_routes.js looks like this: 并且note_routes.js看起来像这样:

module.exports = function(app, db) {
  app.post('/notes', (req, res) => {
    // You'll create your note here.
    res.send('Hello')
  });
};

As can be seen, this is a very simple setup. 可以看出,这是一个非常简单的设置。 According to the above-linked tutorial, this should be enough and should respond with Hello . 根据上面链接的教程,这应该足够了,并应使用Hello响应。 However, this is not the case. 然而,这种情况并非如此。

I am almost 100% sure that the port is correct because there is some not-503 response. 我几乎100%确信端口正确,因为有一些 not-503响应。

What's wrong here, and how can I get this to work as intended? 这是怎么回事,如何使它按预期工作?

Your app.post() in note_routes should be app.get() or if you plan on making post requests then you can chain it like 您在note_routes中的app.post()应该是app.get(),或者如果您打算发出发布请求,则可以将其链接起来

app.post()
   .get();

this is wrong on a whole new kind of level. 在全新的层面上,这是错误的。

I would imagine you are starting your server with node server.js 我可以想象您正在使用node server.js启动服务器

firstly, server.js does not require index.js anywhere 首先,server.js在任何地方都不需要index.js

note_routes should contain .get() NOT .post() note_routes应该包含.get()NOT .post()

in your browser you should be visiting http://localhost:3000/notes NOT plain old http://localhost:3000 在浏览器中,您应该访问http:// localhost:3000 / notes不是普通的旧http:// localhost:3000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM