简体   繁体   English

节点JS:无法获取/

[英]Node JS: Cannot get/

I know there are already different variations of the same question, however, mine is little different. 我知道同一问题已经存在不同的变体,但是,我的变化不大。 I am getting this error cannot get/, this is my node js code 我收到此错误无法获取/,这是我的节点js代码

// set up ======================================================================
var express = require('express');
var path = require('path');
var app = express();                        // create our app w/ express
var mongoose = require('mongoose');                 // mongoose for mongodb
var port = process.env.PORT || 8080;                // set the port
//var database = require('./config/database');          // load the database config
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var io = require('socket.io');
var messageId = {};
    http = require('http'),
    server = http.createServer(function (req, res) {
       res.writeHead(200,{'content-type':'text/plain'});
        res.write("Sever On");
        res.end();
    }),
    io = io.listen(server);
// configuration ===============================================================
//mongoose.connect(database.localUrl);  // Connect to local MongoDB instance. A remoteUrl is also available (modulus.io)

//app.use(express.static('./public'));      // set the static files location /public/img will be /img for users
//app.use(morgan('dev')); // log every request to the console
//app.use(bodyParser.urlencoded({'extended': 'true'})); // parse application/x-www-form-urlencoded
//app.use(bodyParser.json()); // parse application/json
//app.use(bodyParser.json({type: 'application/vnd.api+json'})); // parse application/vnd.api+json as json
//app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request


// routes ======================================================================
//require('./app/routes.js')(app);

// listen (start app with node server.js) ======================================
app.listen(port);
console.log("App listening on port " + port);

My Package.json 我的Package.json

{
  "name": "node-todo",
  "version": "0.0.1",
  "description": "Simple todo application.",
  "main": "server.js",
  "author": "Scotch",
  "dependencies": {
    "body-parser": "^1.18.2",
    "mongoose": "4.10.8",
    "express": "^4.10.8",
    "method-override": "^2.3.10",
    "morgan": "^1.9.0",
    "socket.io": "^2.0.3"
  }
}

As you might have noticed, I am just trying to create a response using http.createServer(). 您可能已经注意到,我只是尝试使用http.createServer()创建响应。 Please let me know what did I do wrong. 请让我知道我做错了什么。

The below code just creates the server - 以下代码仅创建服务器-

server = http.createServer();

But to send any response to client you must first listen to request ie create routes - 但是要向客户发送任何响应,您必须先侦听请求,即创建路由-

app.get('/', (req, res) => {
  res.send('blah');
}

Other stuff you have written correctly I guess. 我猜你写得正确的其他东西。 I hope it fixes your problem. 我希望它能解决您的问题。

There are two servers created in your script. 您的脚本中创建了两个服务器。 You only need to create any one of them. 您只需要创建其中之一即可。

  1. Core HTTP server with http.createServer() but you haven't bound any port to it. 带有http.createServer()核心HTTP服务器,但尚未绑定任何端口。
  2. You have created an express server( var app = express(); app.listen(port); ). 您已经创建了一个快递服务器( var app = express(); app.listen(port); )。 Here you provided port but haven't provided any routing information. 在这里,您提供了端口,但未提供任何路由信息。

Here is what you can do : 您可以执行以下操作:

1. If you want to use core HTTP server 1.如果要使用核心HTTP服务器

replace your sever initilization with 将您的服务器初始化替换为

server = http.createServer(function (req, res) {
    res.writeHead(200,{'content-type':'text/plain'});
    res.write("Sever On");
    res.end();
}).listen(port),

2. If you want to use express 2.如果要使用快递

Add root route 添加根路由

app.get('/', function (req, res) {
    res.send('hello world')
})

before app.listen(port); app.listen(port);之前app.listen(port);

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

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