我有我的第一个node.js应用程序(本地运行正常) - 但我无法通过heroku(第一次w/heroku)部署它.代码如下.所以我不会写这么多代码,所以我只想说在我的网络中本地运行代码没有问题.
var http = require('http');
var fs = require('fs');
var path = require('path');
http.createServer(function (request, response) {
console.log('request starting for ');
console.log(request);
var filePath = '.' + request.url;
if (filePath == './')
filePath = './index.html';
console.log(filePath);
var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
}
path.exists(filePath, function(exists) {
if (exists) {
fs.readFile(filePath, function(error, content) {
if (error) {
response.writeHead(500);
response.end();
}
else { …Run Code Online (Sandbox Code Playgroud) 我是使用Cloud9 IDE(c9)的新手,到目前为止看起来很棒,除了一些小问题.
我从文档中看到,要启动一个简单的node.js http服务器,你必须传入process.env.PORT来代替常规端口,例如"8080".
节点Hello World 示例:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(process.env.PORT, process.env.IP);
Run Code Online (Sandbox Code Playgroud)
我想知道的是,在c9上,你只能使用javascript/node.js在端口上启动服务吗?或者其他语言也可以正常工作,也许还有其他一些传递端口的方法?特别是python + Twisted?
我上传了一些在本地为我工作的扭曲代码,但不能在c9上工作,因为它试图访问本地端口(已经在使用中).这是错误
twisted.internet.error.CannotListenError: Couldn't listen on any:8080: [Errno 98] Address already in use.
Run Code Online (Sandbox Code Playgroud)
如果可能的话,如何在c9上运行以下示例?
Python + Twisted Hello World 示例
from twisted.web import server, resource
from twisted.internet import reactor
class Simple(resource.Resource):
isLeaf = True
def render_GET(self, request):
return "<html>Hello, world!</html>"
site = server.Site(Simple())
reactor.listenTCP(8080, site)
reactor.run()
Run Code Online (Sandbox Code Playgroud)
对文档和github 问题的初步搜索没有太大的影响.我希望这是可能的,我错过了正确的参数传递.
编辑:更新下面的输出
节点代码 …