1、IDEA安装nodejs插件
2、Vue-clie任建个项目
3、Global,console、process,buffer等少数全局模块直接用,其它模块需要自己安装,如npm install mysql,可使用淘宝镜像
安装淘宝镜像:
npm install -g cnpm --registry=https://registry.npm.taobao.org
再安装几个模块:
cnpm install mysql cnpm install express cnpm install http
4、安装好后会在项目同级目录生成node_modules,内含有安装的各个模块;
5、下面玩玩nodejs的MySQL/http/express模块,语法跟JS差不多,没有bom,dom,它不是前端语言哦,右键运行该js即可.
//测试mysql模块
const mysql = require('mysql');
let pool = mysql.createPool({
user: 'root',
password: '123456',
port: 3306,
host: 'localhost',
database: 'vehicle_v2',
connectionLimit: 1022
});
pool.query('select * from user ', (err, results, fielfs) => {
if (err) throw err;
console.log(results)
for (let user of results) {
console.log(user.user_name+user.phone);
}
});
//nodejs服务器,
console.log("浏览器访问http://localhost:8888/list")
const express=require('express');
let app=new express();
app.listen(8888);
app.get('/list',(req,res)=>{res.send('hello express')});
//测试http模块,调一个Java项目接口
const http=require('http');
let options={
host:'www.baidu.com',
port:80,
path:'/'
}
let req=http.request(options,(res)=>{
res.on('data',(buffer)=>{
console.log(buffer.toString())
});
});
req.end();