Tag: 表示

使用Axios或获取自己的REST Api节点内调用吗?

我设立了几个用express来做某些事情的端点。 现在理想情况下,我想从节点应用程序中调用它们来处理一些数据。 有没有办法从节点实例中自己调用apis?

使用navigation.sendBeacon发送应用程序/ json数据,预检成功但不能发布

我有使用sendBeacon发布数据到服务器的问题。 它工作在铬(因为Chrome似乎跳过预检),但在Firefox中预检(OPTIONS)与请求标头一起发送: Host: example.com User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:53.0) Gecko/20100101 Firefox/53.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate, br Access-Control-Request-Method: POST Access-Control-Request-Headers: content-type Origin: http://example.com Connection: keep-alive 而来自服务器的响应是200: Date: Tue, 23 May 2017 15:53:56 GMT Content-Type: text/html; charset=utf-8 Set-Cookie: __cfduid=df2864c1da991df4abc28d3e73db7ab8f1495554836;expires=Wed, 23-May-18 15:53:56 GMT; path=/; domain=.medialaben.no;HttpOnly X-Powered-By: Express Access-Control-Allow-Origin: * access-control-allow-credentials: true […]

NodeJs和Express响应混合起来

我有一个表格forms的页面,我有一个控制器为一行定义,并根据列值我适当地设置我的控制器内的一些variables。 但是,当我试图一个接一个地向不同列中的同一个控制器发起多个请求时,从节点服务器发送的响应就变得混乱起来。 实际上对列1的响应正在与第3列相混淆(除了设置了一些variables值外,相同的列共享相同的控制器)。 有什么方法可以确保我的反应是安全的,不会相互混淆?

处理GET路由上的POST请求(express.js)

我是新来expression和结合在一起似乎是卡住,似乎是一个简单的问题。 我有一个使用GET的API路线。 路线: app.get('/api/v1/all', getAllWords); 然后在getAllWordscallback函数中,我想检查发送的请求是GET还是POST 。 这是我必须检查请求方法的代码: function getAllWords(request, response) { let reply; if (request.method === 'GET') { console.log('This was a GET request'); // handle GET here… } if (request.method === 'POST') { console.log('This was a POST request'); reply = { "msg": "HTTP Method not allowed" }; response.send(reply) } } 当我使用邮差发送一个GET请求时,它工作得很好。 但是当发送一个POST请求时,我得到了通用express.js“ 不能POST / […]

如何将多个POSTparameter passing给mongodb并获得匹配结果

我有一个100个文档/行的MongoDBcollections书籍 。 我使用POSTMAN做了一个带有2个参数“genre:sci fi”和“language:english”的POST请求。 我想从我的集合books获得一个简单的JSON响应,只包含这个值的文档(在SQL中,我会这样做:SELECT * FROM books WHERE genre =“sci fi”AND language =“english”,但我需要这与nodejs / express / mongodb。) Nodejs,Express和MongoDB运行正常,我已经创build了一个文档集合: api.js var express = require('express'); var app = express(); var bodyParser = require('body-parser'); var mongoose = require('mongoose'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); Book = require('./models/books'); mongoose.connect('mongodb://localhost/booksdb'); var db = mongoose.connection; db.on('connected', function() { console.log('MongoDB is running'); […]

无法响应从快递发送的自定义错误消息

这个问题让我很烦恼,因为我知道这跟我没有正确地理解这个问题有关 – 尽pipe花费了几个小时的时间阅读和尝试不同的东西,但是却很难find答案。 我的问题是这样的,我注册时将用户保存到mongodb数据库,我的模式不允许重复的电子邮件,并发送给我一个错误。 我能够控制台loginterminal中的错误,但我有问题发送回客户端。 或者我遇到了问题,如果它回来,我不太确定在哪两个步骤我失去了访问错误消息。 这是我的POST路线来保存用户: router.post('/users', (req, res) => { let body = _.pick(req.body, ['email', 'password']); let user = new User(body); user.save().then(() => { // this all works and will save the user, if there are no errors return user.generateAuthToken(); }).then((token) => { res.header('Authorization', `Bearer ${token}`).send(user); }).catch((err) => { // This is where […]

所有使用Angular 2(+)和Node / Express服务器的TypeScript项目设置

我正在寻找像这样的项目的最佳做法: 用TypeScript编写的Angular 2(4)客户端 Node / Express后端也用TypeScript编写 客户端和服务器代码使用的一些共享(TypeScript)模型。 客户端/服务器/共享应该分成3个节点项目吗? 或者是否有可能把所有东西都放在一个地方,用什么工具/configuration来实现呢?

App.js GET url参数并发送到routes / index.js

这里是我的url: http:// localhost:3000 /?url = test 我正在运行一个Express,所以在我的app.js我试图得到一个URL参数“testing”发送到我的routes / index.js 我发送一个静态variables没有问题,使用: 代码在app.js app.locals.url = 'a test string'; 并在我的路线/ index.js接收 var url; url = req.app.locals.url; 任何想法如何在app.js我可以有: app.locals.url = {the parameter "test" in the http://localhost:3000/?url=test} 谢谢!

快速请求对象不正确报告https

我试图自动redirect我的网站的用户到https,但为了这样做,我需要知道他们是否已经通过https 访问 ,否则我会进入一个无限的redirect循环。 从search,我试着看这些值 console.log('!!!!!!!!!ROUTING TO REDUX!!!!!!!!', request.secure, request.protocol, request.get('X-Forwarded-Protocol')); 但我看到这个logging 2017-06-23T07:20:10.996750 + 00:00 app [web.1]:!!!!!!!!! ROUTING TO REDUX !!!!!!!! 错误的http undefined 2017-06-23T07:20:11.028280 + 00:00 heroku [router]:at = info method = GET path =“/”host = my-library-io.herokuapp.com request_id = 3fc84fae-02bd-443a-8265- c45def605ba1 fwd =“64.134.226.42”dyno = web.1 connect = 0ms service = 33ms status = 200 bytes = […]

使用传递给express服务器的参数执行一个函数?

我想使用传递给快速调用的参数来调用一个函数。 下面是一个绑定在app.get()内调用一个简单的console.log的例子: // server.js const express = require('express') const app = express() const port = process.env.PORT || 3001 function processRequest(api_key, another_value) { console.log(api_key, another_value) } app.get('/api', function(req, res) { let api_key = req.query.api_key let another_value = req.query.another_value processRequest(api_key, another_value) res.json({api_key, another_value}) }) app.listen(port) console.log("Started server") 和一个简单的testing const axios = require('axios') function test() { axios.get('localhost:3001/api?api_key=testkey&another_value=anothervalue', res […]