Tag: 表示

Express.js 4个路由与router.route不匹配

我试图在Express 4中获得router.route的挂起权限。文档使得它听起来很棒,但是它不适合我。 如果我使用命令行工具制作标准应用程序,然后添加如下所示的routes / contacts.js: var express = require('express'); var router = express.Router(); router.route('/:contactid') .get(function(req, res) { res.send('(get) It worked '+contactid); }) module.exports = router; 然后在app.js中添加: var contacts = require('./routes/contacts'); … app.use('/contacts', contacts); 我期望http://localhost:8000/contacts/1匹配来自contacts.js的路由。 但是,我收到一个错误,基本上表明它不匹配contacts.js中的任何路由 Error: Not Found at Layer.app.use.res.render.message [as handle] (project1/app.js:31:15) at trim_prefix (project1/node_modules/express/lib/router/index.js:226:17) at c (project1/node_modules/express/lib/router/index.js:198:9) at Function.proto.process_params (project1/node_modules/express/lib/router/index.js:251:12) at next (project1/node_modules/express/lib/router/index.js:189:19) […]

如何在ejs / express的全局variables中使用函数?

var app = express(); app.locals.current_time = utils.current_time(); 我可以为模板中的每个请求更改此variables的variablescurrent_time吗? 这个例子中的app.locals.current_time是常量,但我想使用函数。

Node.js /快递员不工作

我对Node.js有一个奇怪的问题。 无法从客户端获得发布请求。 获取请求有效,但由于某种原因,发布请求不起作用。 下面是一些简单的代码给我一个错误: var express = require('express'); var app = express(); app.post('/', function(req, res){ res.end('Express post page!'); }); app.listen(4001); 当我尝试访问浏览器中的页面时,它说: Cannot GET / 再次,当我使用get请求它工作正常,但是当我尝试使用一个post请求,它给了我这个错误。 任何帮助,将不胜感激。 PS我正在使用Express 4.4.5的最新版本

express(4.0)定制中间件在路由器的两个实例上运行,即使它只声明为一个

我正在使用下面的代码来了解一下新的express.js(4.0)。 我似乎无法理解为什么日志logging正在发生,无论我使用浏览器访问哪个path。 不应该只loggingwebsite.get而不是api.getpath? // Express 4.0 test… var express = require('express'); var app = express(); var website = express.Router(); var api = express.Router(); var port = process.env.PORT || 3000; website.use(function (req, res, next) { console.log(req.method, req.url); next(); }); website.get('/', function (req, res) { res.send('Home page'); }); website.get('/about', function (req, res) { res.send('About page'); }); api.get('/', […]

用CouchDB附件提供文件?

我使用Express。 我不知道如何发送一个图像文件到客户端的方式,它会显示为HTML标记<img src='/preview/?doc=xxxxxx&image=img1.jpg'> 。 我正在使用Cradle getAttachment函数与Couchdb进行通信https://github.com/flatiron/cradle db.getAttachment(id, filename, function (err, reply) { set('Content-Type', 'image/png'); res.end(reply); }); 我不知道reply是什么,以及如何将这个图像无缓冲地转移到客户端

通用error handling程序MEANJS,mongoose,温斯顿

所以我最近开始在节点和平均编程,而在与JAVA工作时,我总是专注于最好的模式来创build可靠的应用程序,即使学习曲线正在杀死我,我在这里也一样。 无论如何,我试图实现一个通用的error handling程序使用MEANjs在中央的地方有一个exception处理程序,logging错误到一个文件,并在发生这种情况时发送电子邮件。 以mongoose为例 app.route('/process/:pId').get(function(req, res){ pModel.findById(req.params.pId, function(err, doc){ if(err) {//this is so console.log(err);//repetitive so I would res.send(500, 'Error: error.'); }//like to send all these errors to a central handler like in express.js below res.json(200, {data: doc}); }); };); 这是express.js中的处理程序: app.use(function(err, req, res, next) { if (!err) return next(); // can I call another error […]

Node-MySQL – Query()方法中的转义与Mysql.Escape()/ Mysql.EscapeId()

我目前正在使用node-mysql库将我的应用程序连接到一个MySQL实例。 在阅读了我发现的其他一些StackOverflow问题和文章之后,听起来就像每次调用query()方法时,node-mysql都会自动转义不安全的字符。 但是在一些代码片段中,我还看到在query()方法中调用了mysql.escape()和mysql.escapeId() 。 这似乎是,虽然query()自动转义一些危险的字符,你仍然应该调用mysql.escape()和mysql.escapeId()转义其他危险字符。 它是否正确? 如果是这样,什么样的SQL注入攻击被query()方法自动保护,通过调用mysql.escape()和mysql.escapeId()来保护什么样的SQL注入攻击?

Express.js – pipe理所有路由的单个路由文件

我有点新express.js并没有find一个匹配的答案,我的问题呢。 我有一个像这样的文件夹结构的快速应用程序: app/ |–app.js |–routes/ |–|–index.js |–|–news.js |–|–users.js |–some_other_folders/ 至于现在,我已经在app.js中pipe理了我的路线,如下所示: var index = require('./routes/index'); var users = require('./routes/users'); … app.use('/', index); app.use('/users', users); … 这工作正常,但我原本只想用一个文件来pipe理我所有的路由,所以我只需要在app.js.中引用这个文件。 像这样的东西: [app.js] var routes = require('./routes/routes'); app.use('/', routes); [routes/routes.js] … router.get('/', function(req, res) { res.render('index.html'); }); require('./users'); // This is meant to refer to the users routes … 有没有办法将我的其他路线文件(如用户,新闻等)包含到“主”路由文件中? 还是有其他的最佳做法来pipe理express.js内的路线? […]

Express.js请求参数未定义

我有以下节点/快速代码: app.get("/notes/:categoryName", function (res, req) { var categoryName = req.params.categoryName; res.render("notes", {title: categoryName}); }); 但是当我查看req.params时,它是不确定的。 然而,在另一个控制器,它工作正常: app.get("/api/notes/:categoryName", function (req, res) { var categoryName = req.params.categoryName; data.getNotes(categoryName, function (err, notes) { if (err) { res.send(400, err); } else { res.set("Content-Type", "application/json"); res.send(notes.notes); } }); }); 这是怎么回事?

如何在开发过程中运行多个Node.js应用程序

我试图在我的开发机器上运行两个node.js应用程序,但第二个应用程序在运行时引发以下exception。 有没有办法以并行的方式运行这两个应用程序?