1. 程式人生 > >搭建Node.js Redis開發環境

搭建Node.js Redis開發環境

() func ron 8.0 .com pan color 9.png mil

創建項目

初始化為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

技術分享圖片

搭建Node.js Redis開發環境