在Small Orange上使用Node.js Express应用程序

3
我是新手,正在尝试在"A Small Orange"上运行一个表达式应用程序,按照此链接进行操作。
我首先创建了以下目录结构:
/home/user/servercode/myapp 其中包含tmp目录和app.js文件 /home/user/public_html/clientCode/myapp ,其中包含.htaccess文件
/home/user/servercode/myapp/tmp 中包含一个空的restart.txt文件。在/home/user/servercode/myapp中,我运行了以下命令:
npm init 
npm install express --save

这是我的app.js文件。基本上与帖子中提到的链接中的文件相同。
var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World Express App');
});

if (typeof(PhusionPassenger) != 'undefined') {
    console.log( 'Example app listening with passenger' );
app.listen('passenger');
} else {
    console.log( 'Example app listening with 3000' );
    app.listen(3000);
}

.htaccess的权限级别为644,并包含以下内容

PassengerEnabled on
PassengerAppRoot /home/user/serverCode/myapp
SetEnv NODE_ENV production
SetEnv NODE_PATH /usr/lib/node_modules

当我尝试访问myapp时,浏览器控制台显示无法获取/myapp/并且出现404错误。

下面是使用以下内容在app.js中运行不带express的正常nodejs应用程序:

var http = require('http');
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});
server.listen(8000);
console.log("Server running at http://127.0.0.1:8000/");

但是无法运行Express应用程序

1个回答

2
你需要在应用程序中添加路由,如下所示:
app.get('/myapp', function(req, res){...});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接