所以我在server.js文件中有以下代码,我正在运行node.js. 我正在使用express来处理HTTP请求.
app.post('/api/destinations', function (req, res) {
var new_destination = req.body;
console.log(req.body);
console.log(req.headers);
db.Destination.create(new_destination, function(err, destination){
if (err){
res.send("Error: "+err);
}
res.json(destination);
});
});
Run Code Online (Sandbox Code Playgroud)
我在终端中运行以下内容:
curl -XPOST -H "Content-Type: application/json" -d '{"location": "New York","haveBeen": true,"rating": 4}' http://localhost:3000/api/destinations
Run Code Online (Sandbox Code Playgroud)
运行该server.js后打印出以下内容.
{}
{ host: 'localhost:3000',
'user-agent': 'curl/7.43.0',
accept: '*/*',
'content-type': 'application/json',
'content-length': '53' }
Run Code Online (Sandbox Code Playgroud)
所以req.body是{}.我阅读了其他关于类似问题的Stack Overflow帖子,其中由于正文解析器,内容类型不正确.但这不是问题,因为内容类型是application/json.
任何想法如何获得请求的实际主体?
提前致谢.
我正在尝试使用节点画布裁剪背景图像,但在以下代码中遇到了一些奇怪的错误。
错误是“[错误:写入输出流时出错]”。如果我使用节点画布存储库中的裁剪示例代码,我会收到此错误“空 JPEG 图像(不支持 DNL)”
/* */
var cnv = new Canvas(w,h),
ctx = cnv.getContext('2d'),
original = '/img/pages/'+page+'/background.jpg';
ctx.imageSmoothingEnabled = true;
fs.readFile('public/'+original, function( err,image){
if (err) {
res.status(404)
.send('Not found');
}
else {
var
img = new Image;
img.src = image;
ctx.drawImage(img,
Math.abs( (w-img.width)/2),0,
w,h,
0,0,
w,h
);
cnv.toBuffer(function(err, buf){
console.log(err);
if( err){
res.status(500)
.send('Error generating image buffer');
}
else {
fs.writeFile('public'+resampled, buf, function(err){
console.log(err);
console.log('Resized and saved in %dms', new Date - start);
returnResampledFile( res,page,w,h);
});
}
});
} …Run Code Online (Sandbox Code Playgroud) 我想在以下响应中模拟对obj.key3值的不同响应.就像obj.key3 = true一样,返回与obj.key3 = false不同的响应
function method (obj) {
return anotherMethod({key1: 'val1', key2: obj.key3});
}
Run Code Online (Sandbox Code Playgroud) 我正在构建一个功能,允许用户安排服务器生成报告。
即,用户 A 希望安排在每天下午 5 点生成报告并通过电子邮件发送给他,而用户 B 希望安排在每天上午 10 点生成报告。
可能有许多用户希望作业在他们选择的任何时间运行。
我看到有一个叫做节点计划的东西: https: //www.npmjs.com/package/node-schedule
您可以在哪里执行此操作:
var schedule = require('node-schedule');
var j = schedule.scheduleJob('42 * * * *', function(){
console.log('The answer to life, the universe, and everything!');
});
Run Code Online (Sandbox Code Playgroud)
在每小时 42 分钟时执行 cron 作业。
如果系统只有一个用户,这样的事情可能会起作用,但对于多个用户,我无法硬编码安排作业的时间。有什么建议吗?
我正在使用 Express.js 运行基于 Node.js 的 Restful API 服务器。
今天,我意识到有人可以在他使用 Curl 发送请求时获取我的源代码的错误信息,包括我的服务器的目录路径curl -X POST ~。
我收到的错误消息:
类型错误:无法在回调 (/usr/local/node/XXX/node_modules/express/ lib/router/index.js:124:37) 在参数 (/usr/local/node/XXX/node_modules/express/lib/router/index.js:118:11) 在参数 (/usr/local/node/ XXXv/node_modules/express/lib/router/index.js:125:11)
当服务器显示错误时如何隐藏这些关键信息?
例如,我有整数4060.
我怎么\x34\xC8\x7D\x45能从它获得HEX float()?
JS没有float打字,所以我不知道如何进行这种转换.
谢谢.
我在这里有此代码:
if(fs.existsSync('./example/example.js')){
cb(require('../example/example.js'));
}else{
cb();
}
Run Code Online (Sandbox Code Playgroud)
为什么要fs.existSync使用与之不同的目录require?
这将是目录树,排除不需要的东西...(我正在使用express btw)
\example
example.js
\routes
index.js <-- this is the one where I am using this code
app.js <-- this one requires index.js and calls its functions using app.get('/example',example.index);
Run Code Online (Sandbox Code Playgroud) 如果在套接字上为同一个函数多次调用'on'方法会发生什么?多次调用它只是覆盖了最后一个注册的函数,还是使用了更多的资源?
如果是后者,那么如何确定处理程序是否已经注册?
我有一个产品模型,它有很多领域。其中一些专用于前端应用程序,例如:
var GameSchema = new Schema({
likes: {
type: [{
type: Schema.ObjectId,
ref: 'User'
}]
},
likes_count: {
type: Number
}
});
Run Code Online (Sandbox Code Playgroud)
我不需要likes_countDb 中的字段,但控制器只返回模型具有的字段,所以我将likes_count字段添加到 db 模型
exports.some_method = function(req, res){
var game = req.game;
game.likes_count = game.likes.length
res.json(game);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在发送请求时将额外数据添加到 db 模型中而不将它们放入 db 中?
请注意,问题不在likes_count现场本身,我有不同的模型,但重点是在 db 模型上有额外的数据。
我想在OS X 10.12中运行来自facebook/react-native/Examples的示例.
首先我使用npm install然后使用npm start:
有一个错误:
Error watching file for changes: EMFILE
{"code":"EMFILE","errno":"EMFILE","syscall":"Error watching file for changes:","filename":null}
Error: Error watching file for changes: EMFILE
at exports._errnoException (util.js:1008:11)
at FSEvent.FSWatcher._handle.onchange (fs.js:1406:11)
Run Code Online (Sandbox Code Playgroud)
然后我使用Xcode运行代码,但有相同的错误.
我需要帮助或建议.非常感谢.
javascript ×7
node.js ×7
express ×3
body-parser ×1
canvas ×1
handler ×1
ieee-754 ×1
json ×1
mongodb ×1
mongoose ×1
react-native ×1
reactjs ×1
sinon ×1
socket.io ×1