Tag: http

TypeError:require(…)不是节点js中的函数

我试图学习节点js和尝试几个示例代码,当我碰到socket.io网站,我尝试在他们的网站示例代码。 我不认为他们的网站上写的代码将是错误的。 我在运行代码时遇到的错误,我不知道如何解决,我不知道我做错了什么。 我对node js很新,请帮帮我。 这是我采取代码的网站的链接。 这是我试图运行的代码。 var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); app.get('/', function(req, res){ res.sendfile('index.html'); }); io.on('connection', function(socket){ console.log('a user connected'); }); http.listen(3000, function(){ console.log('listening on *:3000'); }); 而我运行代码时得到的错误是 var io = require('socket.io')(http); ^ TypeError: require(…) is not a function at Object.<anonymous> (/home/ubuntu/workspace/index.js:3:30) at Module._compile (module.js:434:26) at […]

创build无端口的快递服务器

我想知道是否有可能创build一个服务器使用快递服务没有任何端口。 我需要创build一个服务器只是为了与其他应用程序使用TCP协议套接字连接。 也许快递不是最好的工具呢? 因为快递是为web框架,我真的不需要任何端口。 目前我在做: http.createServer(app).listen(8000, function() { console.log('Connected in port 8000'); }); 谢谢。

节点覆盖请求IPparsing

我正在努力find正确的术语来准确地说出这个问题,但我会把它放在最好的位置: 在node.js中,有没有办法在发出HTTP请求时手动覆盖IP地址(例如请求some-domain.com/whatever ,而不是通过DNSparsingIP地址,手动提供一些IP地址1.2.3.4 )? 这实际上相当于在/etc/hosts中设置1.2.3.4 some-domain.com

节点js找不到响应主体http createserver

我在节点上运行这个脚本: var http = require('http'); var server = http.createServer(function (request, response) { response.writeHead(200, { "Content-Type": "text/plain" }); response.write('Hello World\n'); response.end('Goodbye World', 'utf8', function() {console.log(response.body); }); server.listen(8000); console.log('running'); 当我在Chrome中加载页面(localhost:8000)时,我看到: 你好,世界 世界再见 到目前为止这么好,但我试图了解响应对象中的数据('Hello World / nGoodbyeWorld')是什么。 这就是为什么我有'console.log(response.body)'作为response.end()中的callback(节点http文档说callback将在响应完成stream时执行)。 但是console.log只是给了'undefined'。 当我console.log整个响应对象它的console.logs响应对象好,但我看不到任何数据或身体在那里,即使它有'hasBody:true'。 所以问题是: a)有没有回应? 我认为必须有一个,否则什么都不会在浏览器窗口中显示。 b)如果是这样,我怎样才能访问它,为什么我的方式不工作? 我能find的最接近的答案是这样的: nodejs http.get响应中的body在哪里? ,但我尝试添加 response.on('data', function(chunk) { body += chunk; }); response.on('end', function() { […]

使用快递服务特定的url

有一个express应用程序和两个路线,当访问路线/bar时,我该如何服务路线/foo ? app.get("/foo", (req, res) => res.end("Hello World")); app.get("/bar", (req, res) => /* ??? */); 我不想使用res.redirect("/foo")redirect,因为这将改变url。 从我看到的connect-middleware可以完成这项工作,但是对于我所需要的,它太复杂了。 我只需要将请求转发到/foo路由,并将其提供给/bar路由下的客户端。 我怎样才能做到这一点,如当我打开/bar浏览器中,我会回来"Hello World" ? 我也不想要一个正则expression式的解决scheme。 我想要一个像这样的函数: res.serveUrl("/foo") 。

在Node.js中validationAPIinput的最佳实践?

我正在开发一个移动应用程序,后端在Node.js中。 用户将几乎完全通过移动应用程序与平台进行交互。 作为后端的一部分,我公开了多个API供移动应用程序使用 – 例如:创build账户,发送消息,发布图片等的API。 validationAPIinput的最佳实践是什么? 我的想法是为每个API创build一个模块,其目的是从http请求中提取,消毒和validation相关属性。 例如,“创build账户”API将具有关联的AccountCreationRequest模块,其中具有validate方法,其中将定义所有账户创build特定的validation。 然后,每个特定的validation可以由诸如expressionvalidation器和validation器的库执行。 exports.AccountCreationRequest = { init: function(request) { … extract attributes … }, sanitizeAndValidate: function() { … use express-validator/validator on attributes such as username, email, etc … }, isValid: function() { … return result of validation … } }; 然后,当后端API收到请求时, var accountCreationRequest = AccountCreationRequest.init(httpRequest); accountCreationRequest.sanitizeAndValidate(); if (accountCreationRequest.isValid()) { […]

http模块(具有browserify的node.js)不会使用PATCH方法写请求主体

我一直在开发一个Web客户端来与REST API服务器交互,并且想要使用PATCH方法。 尽pipe我已经试图将请求主体写入PATCH的请求,但是我发现主体仍然是空的。 虽然PUT或POST工作正常。 我可以用PUT来代替,但是有人知道我的http模块的用法是错的吗? 先谢谢你。 var http = require('http'); module.exports = { patch: function(path, data, done, fail){ var jsonData = JSON.stringify(data); var options = { headers: { 'Content-Type':'application/json;charset=UTF-8', 'Content-Length':jsonData.length, } }; var req = this.request(path, "PATCH", done, fail, options); // THIS CODE DOESN'T WRITE jsonData INTO REQUEST BODY req.write(jsonData); req.end(); }, request: function(path, method, […]

replaceNode.js中的$ .ajax()

我一直在使用jQuery的$ .ajax()function,使从我的节点服务器到其他外部API服务器的asynchronousHTTP请求。 我现在想升级我的图书馆,但我越来越 …. /jquery/node_modules/jsdom/lib/jsdom/level1/core.js:418 set nodeName() { throw new core.DOMException();}, ^^ SyntaxError: Setter must have exactly one formal parameter. 我该如何解决这个错误? 有没有像$.ajax()一样灵活的库? 我特别感兴趣的承诺和$ .ajaxPrefilter()function。

如何在Angular 2中进行正确的http post调用?

我刚开始使用Angular 2,我想对我的node.js服务器进行一个post调用来更新数据。 这是我服务中的代码: updateEmployee(id : string, firstName? : string, lastName? : string){ let body : string = JSON.stringify({ "firstName" : "test", "lastName" : "testtwo"}); return this.http.post(this._employeesUrl + id, body) .map(res => res.json()) .catch(this.handleError); } 我的API路线代码: exports.update = function(req, res) { Employee.model.findById(req.params.id).exec(function(err, item) { if (err) return res.apiError('database error', err); if (!item) return res.apiError('not found'); console.log(req); […]

连接到服务器时,浏览器是否自动请求index.html?

我正在使用Node&Express创build一个Web服务器。 我在express实例上设置了express.static指向一个名为public的文件夹,其中包含index.html,但是我还没有设置任何路由。 var express = require("express"); var app = express(); app.use(express.static("public")); 但是,当我用浏览器连接到服务器时,我收到index.html,即使我没有特别要求在url中。 为什么是这样? 如果没有设置path,浏览器是否会自动请求index.html,或者这是Express或Node中的某种默认路由?