gpt4 book ai didi

ruby - 终端 curl : (7) Failed to connect to localhost port 3000: Connection refused

转载 作者:数据小太阳 更新时间:2023-10-29 07:58:46 27 4
gpt4 key购买 nike

我运行时使用的是 OSX Elcapitan,10.11.6

curl localhost:3000

curl localhost:3000 -4

curl 0.0.0.0:3000

curl -x "" "http://127.0.0.1:3000"

它总是说:

curl: (7) Failed to connect to 127.0.0.1 port 3000: Connection refused

ruby -v

ruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-darwin13.0]

我的代码:

var http = require('http'),
express = require('express'),
path = require('path'),
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
CollectionDriver = require('./collectionDriver').CollectionDriver;

var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));

var mongoHost = 'localHost';
var mongoPort = 27017;
var collectionDriver;

var url = 'mongodb://localhost:27017/mongo-server';
// Use connect method to connect to the server
MongoClient.connect(url, function(error, db) {
if (error) {
console.error(error.message);
process.exit(1);
}

console.log("Connected to MongoDB successfully.");
collectionDriver = new CollectionDriver(db);
});

app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function (req, res) {
res.send('<html><body><h1>Hello World</h1></body></html>');
});

app.get('/:collection', function(req, res) {
var params = req.params;
collectionDriver.findAll(req.params.collection, function(error, objs) {
if (error) { res.send(400, error); }
else {
if (req.accepts('html')) {
res.render('data',{objects: objs, collection: req.params.collection});
} else {
res.set('Content-Type','application/json');
res.send(200, objs);
}
}
});
});

app.get('/:collection/:entity', function(req, res) {
var params = req.params;
var entity = params.entity;
var collection = params.collection;
if (entity) {
collectionDriver.get(collection, entity, function(error, objs) {
if (error) { res.send(400, error); }
else { res.send(200, objs); }
});
} else {
res.send(400, {error: 'bad url', url: req.url});
}
});

app.post('/:collection', function(req, res) {
var object = req.body;
var collection = req.params.collection;
collectionDriver.save(collection, object, function(err,docs) {
if (err) { res.send(400, err); }
else { res.send(201, docs); }
});
});

app.put('/:collection/:entity', function(req, res) {
var params = req.params;
var entity = params.entity;
var collection = params.collection;
if (entity) {
collectionDriver.update(collection, req.body, entity, function(error, objs) {
if (error) { res.send(400, error); }
else { res.send(200, objs); }
});
} else {
var error = { "message" : "Cannot PUT a whole collection" }
res.send(400, error);
}
});

app.delete('/:collection/:entity', function(req, res) {
var params = req.params;
var entity = params.entity;
var collection = params.collection;
if (entity) {
collectionDriver.delete(collection, entity, function(error, objs) {
if (error) { res.send(400, error); }
else { res.send(200, objs); }
});
} else {
var error = { "message" : "Cannot DELETE a whole collection" }
res.send(400, error);
}
});

app.use(function (req,res) {
res.render('404', {url:req.url});
});

http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});

最佳答案

看起来您没有启动您的服务器,或者它在您期望的端口以外的端口上运行。

如果这是一个 Rails 应用程序,rails c 让它运行起来。如果它是其他基于 Rack 的程序,您需要以其他方式启动它,例如 Pow .

关于ruby - 终端 curl : (7) Failed to connect to localhost port 3000: Connection refused,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40926198/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com