Tag: 表示

从Angular Controller调用节点函数的常用方法

让我加上这个,我更新Node / Express。 我有一个AngularJS应用程序,利用Node.JS来pipe理Azure Blob需求,例如创buildBlob容器,如下所示: function test(containerName) { blobSvc.createContainerIfNotExists(containerName, function (error, result, response) { if (!error) { // Container exists and allows // anonymous read access to blob // content and metadata within this container } }); }; test('blob4'); 从Node中的server.js执行时创build容器的函数按预期工作,并创build一个blob容器。 不过,我需要在AngularJS应用程序中单击创build一个blob容器。 我设想使用导出来访问和执行在Server.js中创build的函数,但看到一些混合的信息,尤其是当Express.js在图片中时,通过AngularJS客户端调用Node.js函数,因为它似乎在一个Angular应用程序必须进行http调用(请参阅本文中的最后一个答案: 从angular度应用程序调用nodejs中的函数 )。 我的问题如下: 1)由于我的应用程序当前使用Node,Express和Angular,我需要使用我的Angular控制器中的http来运行Node函数/如果通过AngularJS客户端调用,Node / Server.js中编写的所有函数都需要执行$ http即使他们不打电话服务,但可能是执行诸如math的function的function? 基于Express的呼叫示例: function MyCtrl($scope, $http) { // […]

表示CSRF令牌validation

我有CSRF令牌的问题。 当我提交一个表单时,会生成一个新的XSRF-TOKEN ,但是我想我生成了两个不同的标记,我有点困惑。 还有一个称为_csrf的标记,所以我在开发人员工具(XSRF-TOKEN和_csrf)中看到两个不同的cookie, _csrf在发布后不会更改。 我想要做的是为每个post请求生成一个新的标记,并检查它是否有效。 有一件事我知道我应该为了安全而做,但是我卡住了。 这是漫长的一天,我刚刚进入Express和NodeJS。 这是我目前的设置。 var express = require('express') , passport = require('passport') , flash = require('connect-flash') , utils = require('./utils') , csrf = require('csurf') // setup route middlewares ,csrfProtection = csrf({ cookie: true }) , methodOverride = require('method-override') , bodyParser = require("body-parser") , parseForm = bodyParser.urlencoded({ extended: false }) , […]

在nodejs express框架的不同端口上运行多个应用程序

我正在运行一个应用程序,在我的Windows机器上的端口3000( http:// localhost:3000 )上使用node.js express框架。 另外我想在不同的端口上运行另一个(第二个)应用程序,如3005或8080等。在我的第一个应用程序中,我没有指定任何端口号,我相信默认expressjs运行在端口3000.但在第二个应用程序中,我提到的端口号码app.listen(8080)在我的app.js. 当我试图启动第二个应用程序时,获取端口3000已经在使用中的错误。 所以我停止了第一个应用程序,然后只有我可以运行第二个应用程序,也是在3000和8080端口上运行。 我不明白为什么它仍然运行在3000仍然我指定的端口8080号码在app.js。 任何人都可以帮助在同一个实例的不同端口上运行这两个应用程序 谢谢

如何使用nodejs和express来设置授权标题

我正在按照本教程设置nodejs,express,mongoose和swig模板的站点: 使用JSON Web令牌validationNode.js API 在本教程中,作者使用Postman在标题中设置标记。 我已经search了几天,以了解如何在我的网站的标题中设置jwt令牌,但它不适合我。

咖啡标记重置卡不是一个function

我已经在nodejs上运行了coffeescript和expressjs,我正在制作一个脚本给你9个随机的纸牌(没有重复的),我做了一个函数resetCards在每次显示之后重置卡片,但是当我运行脚本时给我: TypeError: resetCards is not a function at Object.<anonymous> (/home/zunon/projects/xKoot/router.js:10:1) at Module._compile (module.js:398:26) at Object.Module._extensions..js (module.js:405:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) at Module.require (module.js:354:17) at require (internal/module.js:12:17) at Object.<anonymous> (/home/zunon/projects/xKoot/xkoot.js:6:10) at Module._compile (module.js:398:26) at Object.Module._extensions..js (module.js:405:10) 这是文件router.coffee: express = require 'express' router = express.Router() cards = [] resetCards() router.route '/randomcard' .get (req, res) -> […]

有什么办法可以监视Express.js中的每个响应吗?

我想在express.js中创build一个中间件函数。 它可以监视每个请求和响应。 我创build了一个中间件,但它只能监视请求,而不是响应。 function middlewareFunc (req,res,next) { console.log(req.body , req.params , req.query); next(); }

如何在NodeJs中扩展express js res对象

这是一个简单的路线: // get all users app.get('/api/user/get-all', authenticated.yes, function(req, res) { queryUsers.findAllUsers( function( users ){ res.json( users ); } ); }); 输出将是从“queryUsers.findAllUsers”函数返回的json。 这很好,但是我想把所有的json输出转换成更加严格的格式,所以输出结果是: res.json({ success: true, payload: users }); 手动操作非常简单,但意味着每次都要input很多内容。 是否有可能添加新的function到“res”对象,启用这样的事情: res.jsonSuccess( users ); 和: res.jsonFail( users ); 哪个会输出 res.json({ success: true, payload: users }); 和: res.json({ success: false, payload: users }); 分别。

Node.js:什么是我们在asynchronous编程中回复正确请求的保证?

在所有的大部分node.js教程中,我可以看到这种编码模式。 app.get('/user', function (req, res) { User.findById(req.body.id, function(err, user) { if (err) throw err; res.send(user); }); }); 现在这条线res.send(user)吓我res.send(user) 。 User.findById是asynchronous ,这个api的终点可能会同时发生,我们在回复req的时候有什么保证。 截至目前我正在创buildclosure以保持variables值。 app.get('/user', function (req, res) { User.findById(req.body.id, function(res){ return function(err, user) { if (err) throw err; res.send(user); } }(res)); }); 解决这个问题的最好方法是什么?

使用Express进行多个API调用的静态variables

我想有一个variables,可以保持其最后的API调用的价值。 也就是说,每次我调用/api ,它会将variables计数递增1,当我调用/value ,它会显示count的值。 所以,如果我调用/api 3次,然后调用/value ,它应该打印3。 可能吗? 保持variables抛出多个API调用的值? 我试图避免访问文件和数据库,我相信我可以做到这一点。

Nodejs实际上是asynchronous吗?

我看到这个post: https ://www.codementor.io/nodejs/tutorial/manage-async-nodejs-callback-example-code,运行代码后,我确认nodejs是asynchronous的。 但是我创build了2个js文件来再次testingnodejs的asynchronousfunction。 文件1: callback_example.js exports.countless = function(callback){ var date = new Date(); console.log("*" + date.getSeconds()); var x = 0; for(var i = 1; i <= 1000000000; i++){ x++; } callback(x); date = new Date(); console.log("**" + date.getSeconds()); } exports.check = function(callback){ var date = new Date(); console.log(date.getSeconds()); callback(123); date = new Date(); console.log(date.getSeconds()); […]