nodejs

目的

  • 了解nodejs
  • 了解后端
    读取本地文件
    连接数据库
    响应请求

检测是否有node

  • node -v

初始化项目

  • 切换到项目目录
  • npm init -y

使用第三方模块

  • 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

  1. 安装 npm i mysql -S
  2. 导入 const mysql = require(“mysql”)
  3. 创建链接
const conn=mysql.createConnect({
host:"localhost",
user:"root",
password:"",
database:"feed"
})
  1. 连接数据库
    conn.connect(function(err){if(!err){console.log(“数据库连接成功”)}})
  2. 定义sql var sql=“select * from feedback where 1”
  3. 执行 sql
conn.query(sql,function(err,result){
if(!err){
console.log(result)
}
})
  1. 断开数据库
    conn.end(function(){
    console.log(“数据库已断开”)
    })

内置服务器创建

  1. 导入 http const http = require(“http”)
  2. 创建服务
const server = http.createServert(function(req,res){
res.stausCode=200;
res.setHeader("Content-Type","application/json")
res.end('{}')
})
  1. 监听端口
    server.listen(8888,function(){
    console.log(“localhost:8888 启动”)
    })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值