Node.js是一个开源和跨平台的JavaScript运行时环境。
Node.js在浏览器之外使用Chrome V8弓|擎运行的JS环境。
( V8提供了JavaScript执行的运行时环境)
学习目的
- 了解node.js
- 了解后端
读取本地文件 连接数据库的 响应请求 - 了解api接口文档
检测
检测是否安装node
win+r 输出指令
node -v
初始化项目
切换项目目录
npm init -y
使用第三方模块
npm iaxios -S 安装模块
const axios =require(“axios”)导入模块
axios.get(url) .then(res=>{})使用模块
使用自定义模块
定义模块
untils.js module.exports={
max(){}, radomStr(){}
}
导入
const untils=require(‘./utils.js’)
使用
max()
randomStr()
导入
const untils=require(‘./utils.js’)
快捷导出
exports.say=function(){
console.log(“到了吃饭时间”)
}
项目运行
配置命令
package.json->script
"serve":"node main.js"
npm run serve
cmd
进入项目目录 node main/js
mysql命令
查询select
指定列查询
select * from `feedback` where 1
添加查询条件
select `msg`,`name` form `1234` where 1;
select * from `1234` where name=`小曾`
指定列查询
select * from `feedback` where 1
查询msg中的包含山的元素 %代表的是任意字符
select * from `123` where msg like '%山%';
_代表任意一个字符串
select * from `123` where msg like '山_有%';
按时间排序 降序
select * from `123` where 1 order by `datetime` desc
查询 偏移2个 截取3行
select * from `123` where 1 order by `datetime` desc limit 2,3
增加
insert into
修改
update
删除
delete
node操作sql
安装
npm i mysql -S
const mysql =require(“mysql”)
创建连接
const conn = mysql.createConnection({
host: "localhost",//域名
user: "root",//用户名
password: "123456",//密码
database: "feed"//数据库
})
连接数据库
conn.connect(function (err) { if (!err) {console.log("连接成功");}})
定义sql
varsql = "select * from `1234` where 1";
执行sql
conn.query(sql, function (err, result) {
if (!err) {
res.end(JSON.stringify(result))
console.log(res);
}
})
断开数据库
conn.end(function (err) {
if (!err) {
console.log("数据库已经断开");
}
})
内置服务器创建
导入http
const http =require("http")
创建服务
const server = http.createServer(function (req, res) {
// req请求的数据 res响应的数据
// 设置响应码为200
res.statusCode = 200;
// 设置响应头
res.setHeader("content-Type", "application/json");
res.end(`{}`)//返回数据
监听端口
server.listen(8888, function () {
console.log("服务器", "localhost:8888", "启动成功");
})
tips :部分关键词描述较少,是因为简单或者很少用