假设您有一个简单的代码块,如下所示:
app.get('/', function(req, res){
res.send('Hello World');
});
Run Code Online (Sandbox Code Playgroud)
这个函数有两个参数,req并且res,它们分别代表所述请求和响应对象.
另一方面,还有其他函数,第三个参数被调用next.例如,让我们看看以下代码:
app.get('/users/:id?', function(req, res, next){ // Why do we need next?
var id = req.params.id;
if (id) {
// do something
} else {
next(); // What is this doing?
}
});
Run Code Online (Sandbox Code Playgroud)
我无法理解next()它的用途是什么或为什么被使用.在那个例子中,如果id不存在,那next实际上在做什么?
我写了一个模块,我刚刚发布到npm(https://npmjs.org/package/wisp)
所以它从命令行安装很好:
$ npm i -g wisp
但是,当我从命令行运行它时,我一直收到一个错误,即没有安装optimist:
$ wisp
Error: Cannot find module 'optimist'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/usr/local/lib/node_modules/wisp/wisp:12:10)
at Object.<anonymous> (/usr/local/lib/node_modules/wisp/wisp:96:4)
at Module._compile (module.js:449:26)
at Object.exports.run (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:68:25)
at compileScript (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:135:29)
at fs.stat.notSources.(anonymous function) (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/command.js:110:18)
Run Code Online (Sandbox Code Playgroud)
但是,我在package.json中指定为依赖:
{
"name": "wisp",
"author": "Brendan Scarvell <bscarvell@gmail.com>",
"version": "0.1.0",
"description": "Global nodejs file server",
"dependencies": {
"optimist": "~0.3.4"
},
"repository": "git://github.com/tehlulz/wisp",
"bin": {
"wisp" : "./wisp"
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道怎么做才能让这个运行?我知道它与bin部分有关,将可执行文件添加到bin并且该目录中的node_modules为空.不知道如何解决这个问题.
我有客户端javascript设置cookie这个真正奇怪的问题.我正在开发一个小页面的演示,目前使用cookie来存储一些'偏好'.请注意,我不能使用服务器端语言进行此演示或任何第三方jQuery插件.
所以我写了一个javascript对象来设置一个cookie:
var cookie = {
set: function (name,value,exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var value = escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=name + "=" + value;
console.log(document.cookie);
}
}
cookie.set('foo','bar',2);
console.log(document.cookie);
Run Code Online (Sandbox Code Playgroud)
它只返回一个空字符串.我已经进入Chrome控制台,看看我是否可以通过直接修改来实现document.cookie
> document.cookie = "foo=bar";
"foo=bar"
> document.cookie
""
Run Code Online (Sandbox Code Playgroud)
你如何通过客户端javascript设置cookie?
编辑:我未处于隐身模式且已启用Cookie.
- 编辑 -
我写了一些middlware来做到这一点:https://npmjs.org/package/flashify
因此,自Express 3.0发布以来,更改已删除req.flash()
https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x(来源)
所以这是我的问题.他们建议在本地使用req.session.messages来显示闪光灯.
因此,要使视图可以访问会话,我们必须执行以下操作:
nb:咖啡脚本
app.locals.use (req,res) ->
res.locals.session = req.session
Run Code Online (Sandbox Code Playgroud)
我们如何从视图中访问会话数据然后清除它?我们无法在视图渲染后清除会话的内容,但是我们无法清除它,因为它不会到达视图所以我对如何解决这个问题有点失落?
试图解决这个问题时有点头疼.我想要做的是有一个带有参数自定义的setTimeout 与出不必创建一个函数来传递.让我用代码解释一下:
想避免:
function makeTimeout(serial){
serial.close();
}
setTimeout(makeTimeout(sp.name), 250);
Run Code Online (Sandbox Code Playgroud)
我想要做的就是以某种方式通过以下方式调用1个班轮:
setTimeout(function(arg1){ .... }(argument_value), 250);
Run Code Online (Sandbox Code Playgroud)
可以这样做,还是只能通过无参数函数?
我正在阅读这篇文章:http://elegantcode.com/2011/04/06/taking-baby-steps-with-node-js-pumping-data-between-streams/并且在理解流方面遇到了一些麻烦.
引用:
Run Code Online (Sandbox Code Playgroud)"Suppose we want to develop a simple web application that reads a particular file from disk and send it to the browser. The following code shows a very simple and naïve implementation in order to make this happen."
所以代码示例如下:
var readStream = fileSystem.createReadStream(filePath);
readStream.on('data', function(data) {
response.write(data);
});
readStream.on('end', function() {
response.end();
});
Run Code Online (Sandbox Code Playgroud)
当我们可以简单地做到时,为什么我们会使用上述方法:
fs.readFile(filePath, function(err, data){
response.write(data);
response.end();
});
Run Code Online (Sandbox Code Playgroud)
我何时或为何使用溪流?
我有一个简单的你好世界计划的麻烦哈哈!我希望有人可以对此有所了解.
所以收到的错误如下:
$ javac Hello.java
$ java Hello
Exception in thread "main" java.lang.NoSuchMethodError: main
Run Code Online (Sandbox Code Playgroud)
因此,通过错误,我可以看到它显然缺少主要,但它在那里:
class Hello
{
public static void main(String argv)
{
System.out.println("hello world");
}
}
Run Code Online (Sandbox Code Playgroud)
如果有任何帮助,我在Mac OS/X上.
需要一些关于如何正确递归地做这些建议的建议.
基本上我正在做的是进入一堆文本并将其作为JSON返回.
例如:
文本:
q
b
name:rawr
Run Code Online (Sandbox Code Playgroud)
返回:
[
"q",
"b",
{
"name": "rawr"
}
]
Run Code Online (Sandbox Code Playgroud)
以下输入:
q
b
name:rawr:awesome
Run Code Online (Sandbox Code Playgroud)
会返回(输出格式不重要):
[
"q",
"b",
{
"name": {
"rawr": "awesome"
}
}
]
Run Code Online (Sandbox Code Playgroud)
如何修改以下代码以允许以递归方式在对象中包含对象.
var jsonify = function(input){
var listItems = input, myArray = [], end = [], i, item;
var items = listItems.split('\r\n');
// Loop through all the items
for(i = 0; i < items.length; i++){
item = items[i].split(':');
// If there is a value, then split it to create …Run Code Online (Sandbox Code Playgroud) 在红色中使用哈希,我需要存储具有多个字段和值的哈希键.我试过如下:
client.hmset("Table1", "Id", "9324324", "ReqNo", "23432", redis.print);
client.hmset("Table1", "Id", "9324325", "ReqNo", "23432", redis.print);
var arrrep = new Array();
client.hgetall("Table1", function(err, rep){
console.log(rep);
});
Run Code Online (Sandbox Code Playgroud)
输出是: { Id: '9324325', ReqNo: '23432' }
我只得到一个价值.如何获取哈希键中的所有字段和值?如果我错了,请帮助我,让我得到代码.谢谢.
node.js ×5
javascript ×3
express ×2
coffeescript ×1
cookies ×1
java ×1
npm ×1
redis ×1
stream ×1