错误:无法获取/

时间:2015-06-10 13:52:13

标签: node.js express cloud9-ide

我在c9.io ide环境下工作,我在server.js文件中写了下面的代码

var http = require('http');
var path = require('path');

var async = require('async');
var socketio = require('socket.io');
var express = require('express');

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

var router = express();
var server = http.createServer(router);

server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
  var addr = server.address();
  console.log("Server listening at", addr.address + ":" + addr.port);
});

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

// respond with "hello world" when a GET request is made to the homepage
app.get('/', function(req, res) {
  res.render('index.html');
});

app.get('/about', function (req, res) {
  res.send('about');
});

在终端中运行节点server.js后,显示为

的消息
Your code is running at https://nodejs2-mujaffar.c9.io.
Important: use process.env.PORT as the port and process.env.IP as the host in your scripts!

Server listening at 0.0.0.0:8080

但是在访问https://nodejs2-mujaffar.c9.io/ url后 - 它不是渲染视图只显示消息错误:无法获取/

我做错了什么?

请帮忙。

Folder structure of app

1 个答案:

答案 0 :(得分:2)

您似乎已经创建了两个快递实例,这可能是您的问题。

尝试更改:

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

var router = express();
var server = http.createServer(router);

为:

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

var server = http.createServer(app);

此时,您的快速app变量未绑定到您的http服务器。相反,您已经绑定了一个名为router的未使用实例。但是,您已将路线注册到app变量。