【发布时间】:2015-06-10 13:52:13
【问题描述】:
我在 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');
});
在终端中运行 node 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 之后 -- 它不是渲染视图只显示消息错误:无法获取/
我做错了什么?
请帮忙。
【问题讨论】:
-
是client/根目录下的index.html文件吗?我假设您不想只使用直接 HTML 的视图引擎?另外,你有 var app = express() 和 var server = http.createServer 有什么原因吗?
-
是的,我没有使用任何视图引擎。现在只想显示纯 html,我已经编辑了问题并添加了文件夹结构图像 -- 请检查。
-
检查 Varedis 的答案,我打算指出 2 实例问题,但他已经提到了。从您的代码看来,您的 app 变量没有在端口上侦听,但服务器是 2 个不同的实例。
标签: node.js express cloud9-ide