一、环境搭建1、下载node.js

node.js中文网下载地址

npm 创建 typescript react项目 创建nodejs项目_键值对

2、安装及配置node.js

参考几位大神的文章 node.js安装及环境配置windows篇 node.js安装及环境配置Mac篇 node.js安装及环境配置Linux篇

二、创建项目1、创建项目文件夹

npm 创建 typescript react项目 创建nodejs项目_Node_02

2、配置项目

命令行进入项目文件夹

npm 创建 typescript react项目 创建nodejs项目_Node.js_03

初始化项目 npm init

npm 创建 typescript react项目 创建nodejs项目_Node_04

npm 创建 typescript react项目 创建nodejs项目_JS_05

npm 创建 typescript react项目 创建nodejs项目_Node_06

安装框架 npm install express -g

npm 创建 typescript react项目 创建nodejs项目_JS_07

热部署 sudo npm install supervisor -g

npm 创建 typescript react项目 创建nodejs项目_键值对_08

解析请求体(post请求)npm i body-parser –save

npm 创建 typescript react项目 创建nodejs项目_Node.js_09

3、创建server.js
//导入功能模块
var express = require('express');
//创建服务器对象,实例,一个应用程序
var app = express();
var bodyParser = require('body-parser');
//urlencoded() 对url进行编码
//extended:true 是否对编码格式开始高级编码
app.use(bodyParser.urlencoded({ extended: true }));
//express.static("www")托管静态资源,参数是静态资源的文件夹
app.use(express.static("static"));
//listen(端口号,执行方法)用来监听指定端口号
app.listen(8082, function() {
    console.log("服务器启动成功,监听端口:%d", 8082);
});
var users = new Map();
//添加一个请求方式为get的接口
//get(接口路径,执行方法(req,resp))
/**
 * 注册接口
 */
app.get("/register.do", function(req, resp) {
    var username = req.query.username;
    var paw = req.query.paw;
    console.log(users.get(username))
    if(username == undefined || username == "") {
        resp.send("用户名不能为空");
    } else if(paw == undefined || paw == "") {
        resp.send("密码不能为空");
    } else if(users.get(username) != undefined) {
        resp.send("用户已存在");
    } else {
        users.put(username, paw);
        resp.send("注册成功");
    }
});
//添加请求方法为post的接口
/**
 * 登录接口
 */
app.post("/login.do", function(req, resp) {
    console.log(req.body)
    var username = req.body.username;
    var paw = req.body.paw;
    if(username == undefined||username=="") {
        resp.send("用户名不能为空");
    } else if(paw == undefined||paw=="") {
        resp.send("密码不能为空");
    } else if(users.get(username) == undefined) {
        resp.send("用户不存在");
    } else {
        var userPaw = users.get(username);
        if(userPaw==paw){
            resp.send("欢迎欢迎,热烈欢迎领导视察!");
        }else{
            resp.send("密码错误");
        }
    }
});

function Map() {
    /** 存放键的数组(遍历用到) */
    this.keys = new Array();
    /** 存放数据 */
    this.data = new Object();

    /**   
     * 放入一个键值对   
     * @param {String} key   
     * @param {Object} value   
     */
    this.put = function(key, value) {
        if(this.data[key] == null) {
            this.keys.push(key);
        }
        this.data[key] = value;
    };

    /**   
     * 获取某键对应的值   
     * @param {String} key   
     * @return {Object} value   
     */
    this.get = function(key) {
        return this.data[key];
    };

    /**   
     * 删除一个键值对   
     * @param {String} key   
     */
    this.remove = function(key) {
        this.keys.remove(key);
        this.data[key] = null;
    };

    /**   
     * 遍历Map,执行处理函数   
     *    
     * @param {Function} 回调函数 function(key,value,index){..}   
     */
    this.each = function(fn) {
        if(typeof fn != 'function') {
            return;
        }
        var len = this.keys.length;
        for(var i = 0; i < len; i++) {
            var k = this.keys[i];
            fn(k, this.data[k], i);
        }
    };

    /**   
     * 获取键值数组(类似<a href="" class='replace_word' title="Java 知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>的entrySet())   
     * @return 键值对象{key,value}的数组   
     */
    this.entrys = function() {
        var len = this.keys.length;
        var entrys = new Array(len);
        for(var i = 0; i < len; i++) {
            entrys[i] = {
                key: this.keys[i],
                value: this.data[i]
            };
        }
        return entrys;
    };

    /**   
     * 判断Map是否为空   
     */
    this.isEmpty = function() {
        return this.keys.length == 0;
    };

    /**   
     * 获取键值对数量   
     */
    this.size = function() {
        return this.keys.length;
    };

    /**   
     * 重写toString    
     */
    this.toString = function() {
        var s = "{";
        for(var i = 0; i < this.keys.length; i++, s += ',') {
            var k = this.keys[i];
            s += k + "=" + this.data[k];
        }
        s += "}";
        return s;
    };
}
4、启动项目

supervisor server.js

npm 创建 typescript react项目 创建nodejs项目_JS_10

5、测试项目

npm 创建 typescript react项目 创建nodejs项目_JS_11

npm 创建 typescript react项目 创建nodejs项目_Node.js_12

6、项目完整结构

npm 创建 typescript react项目 创建nodejs项目_键值对_13