首页
\
问答
\
带有简单redis应用程序的Node.js抛出“未处理的错误”(Node.js with simple redis application throwing 'unhandled error')
带有简单redis应用程序的Node.js抛出“未处理的错误”(Node.js with simple redis application throwing 'unhandled error')
我想从http://expressjs.com/guide.html尝试示例应用程序,所以我写了以下内容:
var express = require('express') var redis = require('redis') var db = redis.createClient(); var app = express(); app.use(function(req, res, next){ var ua = req.headers['user-agent']; db.zadd('online', Date.now(), ua, next); }); app.use(function(req, res, next){ var min = 60 * 1000; var ago = Date.now() - min; db.zrevrangebyscore('online', '+inf', ago, function(err, users){ if (err) return next(err); req.online = users; next(); }); }); app.get('/', function(req,res){ res.send(req.online.length + ' users online'); }); app.listen(3000);
但在尝试启动之后我得到了
events.js:71 throw arguments[1]; // Unhandled 'error' event ^ Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED at RedisClient.on_error (/home/USER/programming/nodejs/express1/node_modules/redis/index.js:148:24) at Socket.<anonymous> (/home/USER/programming/nodejs/express1/node_modules/redis/index.js:83:14) at Socket.EventEmitter.emit (events.js:96:17) at Socket._destroy.self.errorEmitted (net.js:329:14) at process.startup.processNextTick.process._tickCallback (node.js:244:9)
我使用
node.js
0.8.18和以下包:app@0.0.1 /home/USER/programming/nodejs/express1 ├─┬ express@3.1.0 │ ├── buffer-crc32@0.1.1 │ ├── commander@0.6.1 │ ├─┬ connect@2.7.2 │ │ ├── bytes@0.1.0 │ │ ├── formidable@1.0.11 │ │ ├── pause@0.0.1 │ │ └── qs@0.5.1 │ ├── cookie@0.0.5 │ ├── cookie-signature@0.0.1 │ ├── debug@0.7.2 │ ├── fresh@0.1.0 │ ├── methods@0.0.1 │ ├── mkdirp@0.3.3 │ ├── range-parser@0.0.4 │ └─┬ send@0.1.0 │ └── mime@1.2.6 └── redis@0.8.2
I wanted to try out example application from http://expressjs.com/guide.html so I wrote the following :
var express = require('express') var redis = require('redis') var db = redis.createClient(); var app = express(); app.use(function(req, res, next){ var ua = req.headers['user-agent']; db.zadd('online', Date.now(), ua, next); }); app.use(function(req, res, next){ var min = 60 * 1000; var ago = Date.now() - min; db.zrevrangebyscore('online', '+inf', ago, function(err, users){ if (err) return next(err); req.online = users; next(); }); }); app.get('/', function(req,res){ res.send(req.online.length + ' users online'); }); app.listen(3000);
but after trying to launch it I get
events.js:71 throw arguments[1]; // Unhandled 'error' event ^ Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED at RedisClient.on_error (/home/USER/programming/nodejs/express1/node_modules/redis/index.js:148:24) at Socket.<anonymous> (/home/USER/programming/nodejs/express1/node_modules/redis/index.js:83:14) at Socket.EventEmitter.emit (events.js:96:17) at Socket._destroy.self.errorEmitted (net.js:329:14) at process.startup.processNextTick.process._tickCallback (node.js:244:9)
I use
node.js
0.8.18 and following packages:app@0.0.1 /home/USER/programming/nodejs/express1 ├─┬ express@3.1.0 │ ├── buffer-crc32@0.1.1 │ ├── commander@0.6.1 │ ├─┬ connect@2.7.2 │ │ ├── bytes@0.1.0 │ │ ├── formidable@1.0.11 │ │ ├── pause@0.0.1 │ │ └── qs@0.5.1 │ ├── cookie@0.0.5 │ ├── cookie-signature@0.0.1 │ ├── debug@0.7.2 │ ├── fresh@0.1.0 │ ├── methods@0.0.1 │ ├── mkdirp@0.3.3 │ ├── range-parser@0.0.4 │ └─┬ send@0.1.0 │ └── mime@1.2.6 └── redis@0.8.2
原文:https://stackoverflow.com/questions/14799112
更新时间:2024-05-04 22:05
最满意答案
您必须定义自定义格式。
import org.json4s.{FieldSerializer, DefaultFormats} import org.json4s.native.Serialization.write case class Door(override val name: String) extends Thing trait Thing { val name: String val created: DateTime = DateTime.now } implicit val formats = DefaultFormats + FieldSerializer[Door with Thing()] val obj = new Door("dooor") write(obj)
You have to define a custom format.
import org.json4s.{FieldSerializer, DefaultFormats} import org.json4s.native.Serialization.write case class Door(override val name: String) extends Thing trait Thing { val name: String val created: DateTime = DateTime.now } implicit val formats = DefaultFormats + FieldSerializer[Door with Thing()] val obj = new Door("dooor") write(obj)
相关问答
更多-
如何使用Json4s在案例类中表示嵌套的JSON对象?(how to represent nested JSON object in a case class using Json4s?)[2023-06-16]
我不知道你在做什么来获得你发布的例外,但是下面的工作(注意Map而不是List ): import org.json4s._ import org.json4s.jackson.JsonMethods._ import org.json4s.DefaultFormats val json = parse(""" { "key":"01234", "eventProperties":{ "unknownProperty1":"value", "unknownProperty2":"val ... -
json4s中是否有UUID的盒子(de)序列化程序?(Is there an out of the box (de)serializer for UUID in json4s?)[2022-06-10]
由于我的pull请求被合并,所以有JavaTypesSerializers。 用法: implicit lazy val formats = Serialization.formats(NoTypeHints) ++ org.json4s.ext.JavaTypesSerializers.all Since my pull request was merged, there is JavaTypesSerializers. Usage: impli ... -
您必须定义自定义格式。 import org.json4s.{FieldSerializer, DefaultFormats} import org.json4s.native.Serialization.write case class Door(override val name: String) extends Thing trait Thing { val name: String val created: DateTime = DateTime.now } implicit val f ...
-
由于类型安全限制,我认为没有一个清晰的解决方案。 不过,如果你依靠Java使用类型擦除这个事实来解决这个问题,那么下面是一个似乎可行的解决方案: class EnumSerializer() extends Serializer[Enum[_]] { override def deserialize(implicit format: Formats): PartialFunction[(TypeInfo, JValue), Enum[_]] = { // using Json4sFakeEnum ...
-
原来json4s缺少正确的序列化器。 也许最好通过例外通知? 无论如何:添加 + FieldSerializer[StatCounter]() 到DefaultFormats修复了这个问题 It turns out json4s was lacking the correct serializer. Maybe it would be better to be notified via an exception? Anyway: adding + FieldSerializer[StatCounter]( ...
-
将java enum转换为scala Enumeration用于json4s序列化(Convert java enum to scala Enumeration for json4s serialization)[2023-10-04]
像这样的东西适合你吗? class EnumSerializer[E <: Enum[E]](implicit ct: Manifest[E]) extends CustomSerializer[E](format ⇒ ({ case JString(name) ⇒ Enum.valueOf(ct.runtimeClass.asInstanceOf[Class[E]], name) }, { case dt: E ⇒ JString(dt.name()) })) // first enum I ... -
在phantom-dsl Collection列上使用json4s的JSON序列化程序(JSON serializer with json4s on phantom-dsl Collection column)[2022-06-11]
我认为你的问题是compact(render(obj))调用没有按预期工作。 幻像示例基于lift-json库,其中精确方法调用生成字符串。 很可能,你的render()方法需要一个JValue ,所以你需要做的是在调用compact(render())之前从JsonVin生成一个JValue 。 您可能会导入import org.json4s.JsonDSL._ ,其中应该有不同类型对象的render方法。 此外,使用Jackson序列化程序时,您可能需要提供自定义序列化程序,如此处所述。 如果可以的话, ... -
Json4s超类的序列化(Json4s serialisation of superclass)[2024-02-01]
为了更好地解析JSON,你可以试试这个: import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GsonUtils { public static String defaultDateTimeFormat = "yyyy-MM-dd'T'HH:mm:ssZ"; private static GsonBuilder gsonBuilder = new GsonBuilder().setDateForma ... -
尝试val dog = read[WildDog](json)而不是val dog = parse(json).extract[WildDog] github页面 - https://github.com/json4s/json4s也有其他一些很好的例子。 编辑: 工作版本: Scala 2.10 && org.json4s.jackson> = 3.5.0 Scala 2.11 && org.json4s.jackson> = 3.2.5 我没有检查Scala 2.10的早期版本,但是当我使用3.2.5时 ...
-
你可能需要这样的东西: 编写shapes数组: archive << (DWORD)shapes.GetCount(); // write number of shapes as DWORD for (int i = 0; i < shapes.GetCount(); i++) { shapes[i]->Serialize(archive); // write shape } 读你的shapes数组: DWORD count; archive >> count; ...