node实例分析
Posted senjer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了node实例分析相关的知识,希望对你有一定的参考价值。
http
const http = require(‘http‘) const querystring = require(‘querystring‘) const server = http.createServer((req, res) => { const method = req.method const url = req.url const path = url.split(‘?‘)[0] const query = querystring.parse(url.split(‘?‘)[1]) // 设置返回格式为 JSON res.setHeader(‘Content-type‘, ‘application/json‘) // 返回的数据 const resData = { method, url, path, query } // 返回 if (method === ‘GET‘) { res.end( JSON.stringify(resData) ) } if (method === ‘POST‘) { let postData = ‘‘ req.on(‘data‘, chunk => { postData += chunk.toString() }) req.on(‘end‘, () => { resData.postData = postData // 返回 res.end( JSON.stringify(resData) ) }) } }) server.listen(8000) console.log(‘OK‘)
mysql
const mysql = require(‘mysql‘) // 创建链接对象 const con = mysql.createConnection({ host: ‘localhost‘, user: ‘root‘, password: ‘Mysql_2018‘, port: ‘3306‘, database: ‘myblog‘ }) // 开始连接 con.connect() // 执行 sql 语句 const sql = `insert into blogs (title, content, createtime, author) values (‘标题C‘, ‘内容C‘,1546871704408, ‘zhangsan‘)` con.query(sql, (err, result) => { if (err) { console.error(err) return } console.log(result) }) // 关闭连接 con.end()
pm2
pm2.conf.json
{ "apps": { "name": "pm2-test-server", "script": "app.js", "watch": true, "ignore_watch": [ "node_modules", "logs" ], "instances": 4, "error_file": "logs/err.log", "out_file": "logs/out.log", "log_date_format": "YYYY-MM-DD HH:mm:ss" } }
package.json
{ "name": "pm2-test", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "test": "echo "Error: no test specified" && exit 1", "dev": "cross-env NODE_ENV=dev nodemon app.js", "prd": "cross-env NODE_ENV=production pm2 start pm2.conf.json" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "cross-env": "^5.2.0", "nodemon": "^1.18.9" } }
redis
const redis = require(‘redis‘) // 创建客户端 const redisClient = redis.createClient(6379, ‘127.0.0.1‘) redisClient.on(‘error‘, err => { console.error(err) }) // 测试 redisClient.set(‘myname‘, ‘zhangsan2‘, redis.print) redisClient.get(‘myname‘, (err, val) => { if (err) { console.error(err) return } console.log(‘val ‘, val) // 退出 redisClient.quit() })
stream
// 标准输入输出 // process.stdin.pipe(process.stdout) // 复制文件 const fs = require(‘fs‘) const path = require(‘path‘) const fileName1 = path.resolve(__dirname, ‘data.txt‘) const fileName2 = path.resolve(__dirname, ‘data-bak.txt‘) const readStream = fs.createReadStream(fileName1) const writeStream = fs.createWriteStream(fileName2) readStream.pipe(writeStream) readStream.on(‘data‘, chunk => { console.log(chunk.toString()) }) readStream.on(‘end‘, () => { console.log(‘copy done‘) })
// 读取服务器文件 const http = require(‘http‘) const fs = require(‘fs‘) const path = require(‘path‘) const fileName1 = path.resolve(__dirname, ‘data.txt‘) const server = http.createServer((req, res) => { if (req.method === ‘GET‘) { const readStream = fs.createReadStream(fileName1) readStream.pipe(res) } }) server.listen(8000)
以上是关于node实例分析的主要内容,如果未能解决你的问题,请参考以下文章