Tag: 表示

打印预期格式的JSON数组

这是从我的表中获取细节的代码。 function listshops(callback) { var array=[3,4];/*shopId*/ async.each(array,function(dat,callback){ async.parallel([ function(callback){ client.connection.query('select * from shop where shopId=?',dat,function(err,data1){ callback(null,data1); }); }, function (callback) { client.connection.query('select * from image where shopId=?',dat,function(err,data2){ callback(null,data2); }); } ], function(err,data) { var result = data.reduce(function(prev, curr) { /*merging the array*/ return prev.concat(curr); }); console.log(result); }); }); } 我有这样的输出: http : //i.stack.imgur.com/NVUAu.png 我想以下面的格式打印我的结果: { […]

奇怪的GET请求

我只使用express框架来使用node.js。 昨天我在日志上看到这些。 我得到的search引擎索引我的网站是可以理解的。 我的问题是这些在快速框架中被视为GET请求? 其中大部分返回404,这是有道理的,但其中2人收到200。

表示res.json()丢失数据

我试图使用快递发送数据,但由于某种原因它丢失了部分数据。 function(err, data) { if (err) console.log(err); console.log(data); res.json(data); } variables数据看起来像(简化): {field1: 'value1', field2: 'value2', field3: {subfield: 'subvalue'}} 但在浏览器中我收到: {field1: 'value1', field2: 'value2', field3: null } 我做错了什么或缺less什么? 更新: 完整的代码 UserReadingData.find({ UserId: { "$exists": true, "$eq": user._id }, InFuture: false, Stopped: false }) .populate('SeriesId', 'SeriesName') .exec(function(err, data) { if (err) console.log(err); console.log(data); res.json(data); }); 浏览器中的SeriesId为空。 更新2: […]

GET请求在客户端到服务器给出错误没有'Access-Control-Allow-Origin'

我正在本地主机上运行我的节点/expressionjs应用程序。 我正在向Instagram的api发送'GET'请求,并不断收到此错误: XMLHttpRequest cannot load https://api.instagram.com/oauth/authorize/?client_id=******&redirect_uri=http://localhost:4000/feed&response_type=code. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4000' is therefore not allowed access. 我在我的服务器中这样请求: app.use(function(req, res, next) { res.header("Access-Control-Allow-Headers: x-requested-with"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); }); app.get('/',function(req,res){ res.redirect(redirected to feed route); }) app.get('/feed',function(req,response) { var code = req.query.code; var url = "https://api.instagram.com/oauth/access_token"; var options = […]

使用Express模块​​作为中间件

我是Express的新手,尝试使用中间件来处理POST请求。 如果我暴露端点,并向API发出请求,一切工作正常。 正确地工作 API / index.js app.post('/api/endpoint', (req, res, next) => { next(); }); server.js app.use(function() { console.log('hello'); // => hello }); 但是,当我尝试用导出函数的模块replace中间件函数时,该函数永远不会被调用。 不工作 API / index.js app.post('/api/endpoint', (req, res, next) => { next(); }); server.js const makeExternalRequest = require('./server/makeExternalRequest'); … console.log(makeExternalRequest, typeof makeExternalRequest); // => [Function] 'function' app.use(makeExternalRequest); 服务器/ makeExternalRequest.js module.exports = function(err, req, […]

使用websocket在cookie中刷新一个jwt标记

我对刷新访问/ jwt令牌的一般stream程感到困惑。 我使用socketcluster作为客户端和服务器之间的主要通信方式。 最初的请求(服务的HTML)是由快递。 客户端可以通过github使用passport.js github策略login passport.use(new githubStrategy({ clientID:'********', clientSecret:'*******', callbackURL:'http://localhost:3000/auth/github/callback' }, function(accessToken,refreshToken,profile,done){ profile.token = jwt.sign({ id:profile.id, username:profile.username },'rush2112') return done(null,profile) } )) 带护照的社交策略似乎使用我忽略的会话,并为客户端login生成一个jwt令牌。 成功login,我使用下面的浏览器cookie中存储jwt app.get('/auth/github',passport.authenticate('github')) app.get('/auth/github/callback',passport.authenticate('github',{failureRedirect:'/fail'}), function(req,res){ var text = JSON.stringify({id_token: req.user.token}) res.cookie('id_token',text,{ expires: new Date(Date.now() + 900000), httpOnly: true }) res.redirect('/') } ) 然后我可以使用socket.request.headers.cookie从套接字访问cookie 然后我继续使用解码 var cookie = decodeURIComponent(socket.request.headers.cookie) var id_token = JSON.parse(cookie.split('=')[1]) […]

如何设置快速静态json文件的分页?

我正在服务一个巨大的静态JSON文件,使用下面的expression式,这是由一个angular度前端消耗。 目前大约需要5分钟才能在浏览器中看到数据。 是否有可能将这个文件分块(它有一个ID键)模拟分页? performance … ReportRouter.route('/list') .get(function(req, res){ //res.setHeader("Content-type", "application/json"); fs.readFile('./file.json','utf8',function(err,data){ if(err) throw err; res.json(JSON.parse(data)); }); }); …

无法通过authentication从supertest上传文件到multer

我正在使用multer来处理我的快速应用程序中的file upload,而且我还使用node-sspi进行ntlm身份validation。 当用curl上传文件时,一切正常。 但是,当我尝试与超级相同,这是行不通的。 Supertest只使用auth或只是上传,但我没有成功与他们在一起工作。 curl命令: curl -u user:pass –ntlm -F upload=@filename http://localhost 超级代码不起作用: request(app) .post('/upload') .auth(username, password) .attach('upload', fileToUpload) .expect(200) 如果我省略了attach或auth – 它的工作原理(当然,我需要禁用服务器端的身份validation,但通常我可以上传) 那么,有人知道如何使用超级用户身份上传文件auth?

使用nodemailer发送邮件后调用res.sendStatus()

所以我在express.js实现了一个POST请求处理程序: .post(function(req, res) { ((data selection and processing from mongo…)) var bcast = mailer.prepareBcast(data[0]); mailer.send(bcast).then( function(success) { console.log(success); res.sendstatus(200); }, function(error) { console.error(error); res.sendStatus(555); } ); ((…)) }); 通过mailer.send()方法发送电子邮件,执行success-callback ,并输出successvariables内容,这是来自nodemailer(与gmail transporter一起使用)的返回值。 问题是res.sendStatus(200)没有执行,我的前端等待发送邮件成功(或不成功)的确认。 我在这里做错了什么? 发送邮件后,我可能不能发回任何东西? 如果这是一个明显的问题,我还是相当新的节点,所以很抱歉…

如果在节点上开发的网站上发生突发请求,该怎么办?

我正在使用Express来构build一个预计会有大量请求的站点(在单个实例中大约有900个)。 我应该怎么做,以避免网站崩溃和最低硬件规格相同。