Tag: node.js

如何分离Node.js和Express 4上的路线?

我想从我的server.js文件分开路由。 我正在关注这个教程Scotch.io http://scotch.io/tutorials/javascript/build-a-restful-api-using-node-and-express-4 如果所有的行都在server.js文件中,它正在工作。 但我没有分开。 我怎样才能做这个工作? server.js // set up ====================================================================== var express = require('express'); var app = express(); var bodyParser = require('body-parser'); // configuration =============================================================== app.use(bodyParser()); var port = process.env.PORT || 8000; var mongoose = require('mongoose'); var database = require('./config/database'); mongoose.connect(database.url); var Video = require('./app/models/video'); // routes ======================================================================= app.use('/api', require('./app/routes/routes').router); // listen (start app […]

通过NodeJS中的Http请求获取json

这是我的模型与JSON响应: exports.getUser = function(req, res, callback) { User.find(req.body, function (err, data) { if (err) { res.json(err.errors); } else { res.json(data); } }); }; 在这里我通过http.request得到它。 为什么我收到(数据)一个string,而不是一个JSON? var options = { hostname: '127.0.0.1' ,port: app.get('port') ,path: '/users' ,method: 'GET' ,headers: { 'Content-Type': 'application/json' } }; var req = http.request(options, function(res) { res.setEncoding('utf8'); res.on('data', function (data) { console.log(data); […]

Nodejsasynchronous系列 – 传递参数到下一个callback

当你使用asynchronous模块时,你怎样才能把前一个callback的parameter passing给下一个呢? 这里是一个来自github文档的例子 async.series({ one: function(callback){ setTimeout(function(){ callback(null, 1); }, 200); }, two: function(callback){ setTimeout(function(){ callback(null, 2); }, 100); } }, function(err, results) { // results is now equal to: {one: 1, two: 2} });

玉内联有条件的

我试图使一切从数组中的第一个元素有一个CSS类使用Jade模板引擎。 我希望能这样做,但没有运气。 有什么build议么? – each sense, i in entry.senses div(class="span13 #{ if (i != 0) 'offset3' }") … a tonne of subsequent stuff 我知道我可以包装代码如下,但据我了解玉的嵌套规则工作,我不得不复制代码或提取到一个Mixin什么的。 – each sense, i in entry.senses – if (i == 0) .span13 … a tonne of subsequent stuff – else .span13.offset3 … identical subsequent stuff 有没有更好的方法来做到这一点?

类名玉的variables

我不能在一个类中设置一个variables名称: .flag_#{ session.locale } #{ session.locale } 我有: <div class="flag_" >en</div> 我想要 <div class="flag_en" >en</div> 谢谢

nodejs httprequest与数据 – 获取错误getaddrinfo ENOENT

更新 – 自我回答我看到一个人必须确保从机器上正确parsingDNS,检查这个代码,以确保URL是可达的nodejs.org/docs/latest/api/dns.html#dns.resolve 原来的问题 我正在编写一个基于节点的程序,在这个程序中,用户可以要求我代表他执行一个httprequest(当然,他们提供了一些数据和方法来调用),但是每次我做一个httprequest,它都会给我一个错误 getaddrinfo ENOENT这是我的代码的外观 function makehttprequest(deviceid, httpaction, httppath,methods, actiondata, callback) { console.log('we are here with httpaction' + httpaction + ' path ' + httppath + ' method ' + methods + ' action data ' + actiondata); //do the http post work, get the data, and call the callback function with return […]

npm“未能parsingjson”

当我试图用npm安装express时,我总是得到以下错误: Failed to parse json No data, empty input at 1:1 File: /root/.npm/inherits/2.0.1/package/package.json Failed to parse package.json data. package.json must be actual JSON, not just JavaScript. This is not a bug in npm. Tell the package author to fix their package.json file. JSON.parse 我究竟做错了什么? sudo npm install -g express 操作系统是Ubuntu 12.04(精确)armhf

如何修复curl:(60)SSL证书:无效的证书链

我得到以下错误运行curl https://npmjs.org/install.sh | sh curl https://npmjs.org/install.sh | sh在Mac OSX 10.9(小牛): install npm@latest curl: (60) SSL certificate problem: Invalid certificate chain More details here: http://curl.haxx.se/docs/sslcerts.html 我该如何解决?

Node.js和Socket.IO – 如何断开连接后立即重新连接

我正在用node.js和socket.io构build一个小的原型。 一切工作正常,我面临的唯一问题是,我的node.js连接将断开连接,我被迫刷新页面,以获得连接并再次运行。 一旦断开连接事件被触发,是否有办法重新build立连接? 据我所知,这是一个普遍的问题。 所以,我正在寻找解决这个问题的最佳实践方法:) 丹非常感谢

找不到模块'coffee-script'

尝试使用TowerJSbuild立一个基本站点作为testing,但运行脚手架生成器时遇到了这个错误。 Macbook:app john$ tower generate scaffold Post title:string body:text belongsTo:user { [Error: Cannot find module 'coffee-script'] code: 'MODULE_NOT_FOUND' } module.js:340 throw err; ^ Error: Cannot find module '/Users/john/Sites/tower/app/app/config/shared/application' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:362:17) at require (module.js:378:17) at Function.Tower.Application.Application.reopenClass.instance (/usr/local/lib/node_modules/tower/lib/tower-application/server/application.js:42:15) at _.extend.namespace (/usr/local/lib/node_modules/tower/lib/tower-support/shared/shared.js:218:30) at GeneratorScaffoldGenerator.Tower.GeneratorResources.buildApp (/usr/local/lib/node_modules/tower/lib/tower-generator/server/resources.js:273:66) at GeneratorScaffoldGenerator.Generator (/usr/local/lib/node_modules/tower/lib/tower-generator/server/generator.js:57:23) at new GeneratorScaffoldGenerator […]