目的
- 了解nodejs
- 了解后端
读取本地文件
连接数据库
响应请求
检测是否有node
初始化项目
使用第三方模块
- npm i aixos -S 安装模块
- const axios =require(“axios”)导入模块
- axios.get(yrl) .then(res=>{})使用模块
使用自定义模块
utils.js
module.exports={
max(){}
randomStr(){}
}
- 导入与使用
导入
const utils = require(“./utils.js”)
使用
utils.max()
utils.randomStr() - 导入与使用
导入
const{max,randomStr}=require(“./utils.js”)
使用
max()
randomStr() - 快捷导出
export.say=function(){
console.log(“到结婚的年龄了嘛”)
}
项目运行
- 配置命令
package.json->script
“serve”:node main.js
npm run serve - cmd
进入项目目录
node run serve
mysql命令
查询 select
- select * from
feedback
where 1; - 指定列查询 select
msg
,name
from feedback
where 1; - 添加查询条件 select * from feedback where name=’ ’
- 查询msg中包含山的元素%代表任意字符 select * from feedback where msg like ‘%山%’
- _代表任意字符串 select * from feedback where msg like ‘山_有%’
- 按时间排序 降序 select * from feedback where 1 order by
datatime
desc - 查询 偏移2个 截取3行 select * from feedback where 1 order by
datatime
desc limit 2,3
增加 insert into
修改 update
删除 delete
node操作sql
- 安装 npm i mysql -S
- 导入 const mysql = require(“mysql”)
- 创建链接
const conn=mysql.createConnect({
host:"localhost",
user:"root",
password:"",
database:"feed"
})
- 连接数据库
conn.connect(function(err){if(!err){console.log(“数据库连接成功”)}}) - 定义sql var sql=“select * from feedback where 1”
- 执行 sql
conn.query(sql,function(err,result){
if(!err){
console.log(result)
}
})
- 断开数据库
conn.end(function(){
console.log(“数据库已断开”)
})
内置服务器创建
- 导入 http const http = require(“http”)
- 创建服务
const server = http.createServert(function(req,res){
res.stausCode=200;
res.setHeader("Content-Type","application/json")
res.end('{}')
})
- 监听端口
server.listen(8888,function(){
console.log(“localhost:8888 启动”)
})