[~]# node node.js
Error: Cannot find module 'socket.io'
[~]# node -v
v0.10.10
Run Code Online (Sandbox Code Playgroud)
socket.io安装:
npm install socket.io
npm WARN package.json policyfile@0.0.4 No repository field.
npm WARN package.json policyfile@0.0.4 'repositories' (plural) Not supported.
npm WARN package.json Please pick one as the 'repository' field
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法来确认socket.emit调用.
socket.emit('message', msg);
Run Code Online (Sandbox Code Playgroud)
我已经看到一种机制,接收器将发送另一个自定义事件作为确认,但这将在我的聊天应用程序中添加数千个传输.请建议一个有效的方法.
从我读到的关于CORS的内容来看,我理解它应该如下工作:
Access-Control-Allow-Origin: *),则浏览器会理解允许发送主要请求并执行此操作.我已经为它设置了一个测试:
Access-Control-*在响应中设置标头简单的HTML页面(由另一个端口上的另一个服务器提供),其中包含以下脚本($代表jQuery):
$.ajax({
type: "GET",
crossDomain: true,
url: "http://local.site.com/endpoint,
success: function (data) {
alert(data);
},
error: function (request, error) {
alert(error);
}
});
Run Code Online (Sandbox Code Playgroud)但是,当我调用此方法时,我在"网络"选项卡中只看到一个GET并且没有预检OPTIONS请求 - Chrome 49和Firefox 33.
以下是Chrome的GET请求详情:
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,ru;q=0.6
Connection:keep-alive
Host:local.adform.com
Origin:http://localhost:7500
Referer:http://localhost:7500/test-page.html
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
Run Code Online (Sandbox Code Playgroud)
和相应的回应:
Access-Control-Allow-Headers:Accept, …Run Code Online (Sandbox Code Playgroud) 我有一些代码:
var bar = foo().then(function success(value) {
// compute something from a value...
}, function failure(reason) {
// handle an error...
});
Run Code Online (Sandbox Code Playgroud)
如何在上下文中将failure函数绑定到this对象bar.我知道我将不得不使用myFunc.bind(this)但我可以替代什么myFunc?
我想在MongoDB中做一个批量插入作业,我在mongoose中找到了两种方法:
一种方法是使用insert:
dataArr = [
{
id: "",
name: ""
}
{
id: "",
name: ""
}
]
Run Code Online (Sandbox Code Playgroud)
Collection.insert(dataArr)
另一种方式是Model.create:
Model.create(dataArr)
Run Code Online (Sandbox Code Playgroud)
两者都可以完成批量插入工作,但它们之间有什么区别?
哪一个效率更高?
ExpressJS中间件req,res,next有像钩子.on和.pipe.
但我正在寻找app.get和app.post方法的钩子.
我正在尝试使用PyObjC(3.0.4)更改Mac(10.10.4)上的DNS服务器.
一切似乎都有效:我收到一个身份验证对话框,提示我的程序正在尝试更改网络设置,并返回提交/应用命令True,这表明它们已成功.
但是,系统设置实际上并未更改:它们保持不变.知道为什么他们不"坚持"?
代码(独立,如果你安装了最新版本的PyObjC,应该可以工作):
#!/usr/bin/env python
import objc
from SystemConfiguration import *
# Open dynamic store and get primary interface
store = SCDynamicStoreCreate(None, 'MyApp', None, None)
primaryif = SCDynamicStoreCopyValue(store, 'State:/Network/Global/IPv4')['PrimaryInterface']
if primaryif:
print "Using %s as primary interface" % primaryif
else:
raise "Can't find primary interface"
# Load SecurityInterface framework to provide SFAuthorization
objc.initFrameworkWrapper(
frameworkName = "SecurityInterface",
frameworkIdentifier = "com.apple.securityinterface",
frameworkPath = objc.pathForFramework("/System/Library/Frameworks/SecurityInterface.framework"),
globals = globals()
)
# Access system preferences
preferences = SCPreferencesCreateWithAuthorization(None, 'MyApp', None, …Run Code Online (Sandbox Code Playgroud) 通过peepcode nodejs视频并在当前快速/节点版本上重新创建应用程序我遇到了一个小问题.
文件结构
site
- apps
- - authentication
- - - views
- - - - login.jade
- - - routes.js
- node_modules
- public
- - images
- - javascripts
- - stylesheets
- routes
- views
app.js
Run Code Online (Sandbox Code Playgroud)
login.jade:
extends /views/layout
block content
form(action='/sessions', method='post')
label
| Username
input(type='text', name='user')
label
| Password
input(type='password', name='password')
input(type='submit', name='submit')
Run Code Online (Sandbox Code Playgroud)
app.js
var express = require('express')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express(); …Run Code Online (Sandbox Code Playgroud) 下面是一个PhantomJS示例,它通过外部网页上的DOM id获取一些元素:
var page = require('webpage').create();
console.log('The default user agent is ' + page.settings.userAgent);
page.settings.userAgent = 'SpecialAgent';
page.open('http://www.httpuseragent.org', function(status) {
if (status !== 'success') {
console.log('Unable to access network');
} else {
var ua = page.evaluate(function() {
return document.getElementById('myagent').textContent;
});
console.log(ua);
}
phantom.exit();
});
Run Code Online (Sandbox Code Playgroud)
我想获取网页的整个HTML源代码...我该怎么做?
所以我在server.js文件中有以下代码,我正在运行node.js. 我正在使用express来处理HTTP请求.
app.post('/api/destinations', function (req, res) {
var new_destination = req.body;
console.log(req.body);
console.log(req.headers);
db.Destination.create(new_destination, function(err, destination){
if (err){
res.send("Error: "+err);
}
res.json(destination);
});
});
Run Code Online (Sandbox Code Playgroud)
我在终端中运行以下内容:
curl -XPOST -H "Content-Type: application/json" -d '{"location": "New York","haveBeen": true,"rating": 4}' http://localhost:3000/api/destinations
Run Code Online (Sandbox Code Playgroud)
运行该server.js后打印出以下内容.
{}
{ host: 'localhost:3000',
'user-agent': 'curl/7.43.0',
accept: '*/*',
'content-type': 'application/json',
'content-length': '53' }
Run Code Online (Sandbox Code Playgroud)
所以req.body是{}.我阅读了其他关于类似问题的Stack Overflow帖子,其中由于正文解析器,内容类型不正确.但这不是问题,因为内容类型是application/json.
任何想法如何获得请求的实际主体?
提前致谢.