搭建Node.js Redis开发环境
Posted 抱影无眠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建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
以上是关于搭建Node.js Redis开发环境的主要内容,如果未能解决你的问题,请参考以下文章