博客
关于我
NodeJs入门知识
阅读量:796 次
发布时间:2023-02-16

本文共 5449 字,大约阅读时间需要 18 分钟。

Node.js???????????

1. Node.js??

Node.js?????Chrome V8???????????JS????????????V8?????????API?????????????Web???????????Node.js??????????????I/O?????????????????????????????????

2. Node.js????

?????Node.js?????????

  • ??Node.js???? https://nodejs.org?????????????
  • ??????????????????????????????
  • ????????Node.js????????? node -v?
  • 3. Node.js????

    ??????Node.js???

  • ?????????????? node yourscript.js?
  • ??????????? ? ??? ? ??? Tab ?????
  • 4. ??????

    ?? fs ?????????

    const fs = require('fs');// ????fs.readFile('day.txt', 'utf-8', function(err, data) {    if (err) {        console.log('??????');    }    console.log(data);});// ????fs.writeFile('dayOut.txt', '?????', function(err) {    if (err) {        console.log('??????');    } else {        console.log('??????');    }});

    5. ????

    ?? path ?????????

    const path = require('path');// ????const finalPath = path.join('/a', '/b/c', '../', './d', 'e');console.log(finalPath);// ??????fs.readFile(path.join(__dirname, 'day.txt'), 'utf-8', function(err, data) {    console.log(data);});

    6. HTTP????

    Node.js??? http ???????????Web????

    const http = require('http');const server = http.createServer();server.on('request', function(req, res) {    console.log('??????');    res.end('return data');});server.listen(80, function() {    console.log('??????localhost');});

    7. Express????

    Express ??????Node.js Web????????Web???API?

    const express = require('express');const app = express();app.listen(80, function() {    console.log('??? http://localhost ???...');});

    8. ??????

    Express ??????????????

    const express = require('express');const app = express();const router = express.Router();router.get('/user/list', function(req, res) {    res.send('?????? get /user/list');});app.use('/public', express.static('../day1'));app.listen(80, function() {    console.log('??? http://localhost ???...');});

    9. ????????

    9.1 MySQL??

    ??MySQL??????

    const mysql = require('mysql');const conn = mysql.createConnection({    host: 'localhost',    port: 3306,    user: 'root',    password: 'password',    database: 'mydb'});conn.connect(function(err) {    if (err) {        console.log('????');    } else {        console.log('????');    }});

    9.2 Session??

    Session ???????????

    const session = require('express-session');const app = express();app.use(session({    secret: 'your secret key',    resave: false,    saveUninitialized: true,    cookie: {}}));

    9.3 JWT??

    JWT ???????????

    const jwt = require('jsonwebtoken');const token = jwt.sign({ id: 123 }, 'your secret', {    expiresIn: '1h'});// ?? tokenconst decodedToken = jwt.decode(token, 'your secret');

    10. ???????

    10.1 ????

    Node.js ?????????????????????

    • ?????fs?path?http
    • ???????????JS??
    • ???????? npm ??

    10.2 ???

    ?? npm ????

    // ???npm install express// ???npm uninstall express// ????npm install express -g

    10.3 ???

    ? npm ????????

  • ?? npm ????? https://www.npmjs.com?
  • ???npm login?
  • ????npm publish?
  • 10.4 ??????

    Node.js ???????

  • ?????????
  • ???????? require() ? module.exports?
  • ?????? node_modules ???
  • 11. ??????

    11.1 ????

    ?? npm ???? registry.npmmirror.com?

    npm config set registry https://registry.npmmirror.comnpm install --save-dev nrmnrm use taobao

    11.2 Express??????

    ?? express.static ???????

    const express = require('express');const app = express();app.use(express.static('../public'));app.listen(80, function() {    console.log('????????');});

    11.3 CORS ????

    ????? CORS ????

    const cors = require('cors');const app = express();app.use(cors());app.listen(80, function() {    console.log('???????');});

    12. ????

    12.1 Express API??

    ???? API?

    const express = require('express');const app = express();app.get('/api/test', function(req, res) {    res.send('API????');});app.listen(80, function() {    console.log('API??? http://localhost ???...');});

    12.2 ?????

    ?? Express ? MySQL ???????

    const express = require('express');const mysql = require('mysql');const app = express();const conn = mysql.createConnection({    host: 'localhost',    port: 3306,    user: 'root',    password: 'password',    database: 'mydb'});app.use('/api/test', function(req, res) {    const query = 'SELECT id FROM users';    conn.query(query, function(err, results) {        if (err) {            res.send('???????');        } else {            res.send(results);        }    });});app.listen(80, function() {    console.log('???????? http://localhost ???...');});

    12.3 JWT????

    ?? JWT ? Express ???

    const express = require('express');const jwt = require('jsonwebtoken');const app = express();app.use(express.json());// ?? tokenfunction verifyToken(req, res, next) {    const token = req.headers['x-access-token'];    jwt.verify(token, 'your secret', function(err, user) {        if (err) {            res.status(403).json({ error: '???token' });        } else {            next();        }    });}app.use('/api/auth', verifyToken);app.listen(80, function() {    console.log('JWT????? http://localhost ???...');});

    13. ????

    13.1 Express ???

    ?? nodemon ??????

    npm install -g nodemonnodemon Main.js

    13.2 ????

    ???????

    const express = require('express');const router = express.Router();router.get('/user/list', function(req, res) {    res.send('????');});module.exports = router;

    13.3 ?????

    ???????

    const express = require('express');const app = express();app.use((req, res, next) => {    console.log('????????');    next();});app.get('/', function(req, res) {    res.send('?????????');});app.listen(80, function() {    console.log('?????? http://localhost ???...');});

    ?????????????Node.js????????????????Web???API?????

    转载地址:http://mtjfk.baihongyu.com/

    你可能感兴趣的文章
    Nodejs中的fs模块的使用
    查看>>
    NodeJS使用淘宝npm镜像站的各种姿势
    查看>>
    NodeJs入门知识
    查看>>
    nodejs包管理工具对比:npm、Yarn、cnpm、npx
    查看>>
    NodeJs单元测试之 API性能测试
    查看>>
    nodejs图片转换字节保存
    查看>>
    nodejs在Liunx上的部署生产方式-PM2
    查看>>
    nodejs基于art-template模板引擎生成
    查看>>
    nodejs字符与字节之间的转换
    查看>>
    NodeJs学习笔记001--npm换源
    查看>>
    NodeJs学习笔记002--npm常用命令详解
    查看>>
    nodejs学习笔记一——nodejs安装
    查看>>
    vue3+Element-plus icon图标无法显示的问题(已解决)
    查看>>
    NodeJS实现跨域的方法( 4种 )
    查看>>
    nodejs封装http请求
    查看>>
    nodejs常用组件
    查看>>
    nodejs开发公众号报错 40164,白名单配置找不到,竟然是这个原因
    查看>>
    Nodejs异步回调的处理方法总结
    查看>>
    NodeJS报错 Fatal error: ENOSPC: System limit for number of file watchers reached, watch ‘...path...‘
    查看>>
    nodejs支持ssi实现include shtml页面
    查看>>