搭建Node.js Redis开发环境

创建项目

初始化为node项目

$npm init

 

安装redis

 

安装@types/node, @types/redis, typescript

 

初始化TypeScript

 

配置tsconfig.json

 

 

参考package.json

{

"name": "redis-demo",

"version": "1.0.0",

"description": "",

"main": "app.js",

"scripts": {

"build": "tsc",

"dev": "tsc -w",

"start": "node .\\build\\app.js",

"test": "echo \"Error: no test specified\" && exit 1"

},

"author": "",

"license": "ISC",

"devDependencies": {

"@types/node": "^9.6.0",

"@types/redis": "^2.8.6",

"typescript": "^2.7.2"

},

"dependencies": {

"redis": "^2.8.0"

}

}

 

新建文件App.ts

import * as redis from "redis"

 

console.log("redis node.js demo!");

 

let client = redis.createClient();

 

client.on("error", function (err) {

console.log("Error " + err);

});

 

client.set("hello", "redis", redis.print);

client.get("hello", (err, reply) => {

console.log("Error %s ", err);

console.log("Reply %s ", reply);

});

 

 

编译App

$ npm run dev

 

运行App

$ npm start

 

 

参考资源:

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/redis/redis-tests.ts

https://npm.taobao.org/package/redis

 

 

 

 

 

posted @ 2018-03-25 14:30  抱影无眠  阅读(586)  评论(0编辑  收藏  举报