nodejs,,一些基本操作--server。js
1.解决中文乱码问题:
const http = require('http')
const server = http.createServer((req, res) => {
// 设置字符编码 - 设置文本的解析形式
// 200 表示本次请求是成功的
// text/html ---- 解析标签输出
// text/plain ---- 转义标签输出 ---- 原样输出
// nodejs服务器 默认是 text/html
res.writeHead(200, {
'Content-Type': 'text/plain;charset=utf-8'
})
res.write('<h1>hello world</h1>')
res.write('<h2>您好</h2>') // 如果出现中文字符,默认会乱码 ---- 设定字符编码
res.end()
})
server.listen(3000)
console.log('your server is runing at http://localhost:3000')
// ???? 打开控制台 -》 NETWORK,刷新页面,发现有2个请求 --- 要的是一个请求 ---- 过滤不需要的请求2.
2.解析网址(自己设置的网址)
// http
// 解析网址 url
/**
* 完整的URL地址的解析字段
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ href │
├──────────┬──┬─────────────────────┬────────────────────────┬───────────────────────────┬───────┤
│ protocol │ │ auth │ host │ path │ hash │
│ 协议 │ │ ├─────────────────┬──────┼──────────┬────────────────┤ │
│ │ │ │ hostname │ port │ pathname │ search │ │
│ │ │ │ 域名 │ 端口 │ 路由 ├─┬──────────────┤ │
│ │ │ │ │ │ │ │ query │ │
" https: // user : pass @ sub.example.com : 8080 /p/a/t/h ? query=string #hash "
│ │ │ │ │ hostname │ port │ │ │ │
│ │ │ │ ├─────────────────┴──────┤ │ 参数 │ 哈希值│
│ protocol │ │ username │ password │ host │ │ │ │
├──────────┴──┼──────────┴──────────┼────────────────────────┤ │ │ │
│ origin │ │ origin │ pathname │ search │ hash │
├─────────────┴─────────────────────┴────────────────────────┴──────────┴────────────────┴───────┤
│ href │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
*
*/
const url = require('url') const urlStr = "http://localhost:3000/login?username=wudaxun&password=123456" // url.parse(urlStr[, boolean]) --- 将字符串类型的网址转换成对象 const obj = url.parse(urlStr,true) // console.log(obj) /**
* Url {
protocol: 'http:', // 协议
slashes: true,
auth: null, // 作者
host: 'localhost:3000', // 域名 + 端口
port: '3000', // 端口
hostname: 'localhost', // 域名
hash: null, // 哈希值
search: '?username=wudaxun&password=123456', // ? + 参数
query: 'username=wudaxun&password=123456', // 字符串类型的参数
pathname: '/login', // 路由
path: '/login?username=wudaxun&password=123456', // 路由 + ? + 参数
href: 'http://localhost:3000/login?username=wudaxun&password=123456' // 完整的地址
}
*/ console.log(obj.query) // username=wudaxun&password=123456
console.log(obj.query.username) //wudaxun
console.log(obj.query.password) //123456
2.1.过滤图标设置图标
/**
* 过滤图标 、设置路由
*/
const http = require('http');
const url = require('url'); // 获取前端请求的信息 --- url.parse() 字符串的url转成对象 const server = http.createServer((req, res) => {
if (req.url !== '/favicon.ico') { // 过滤图标的请求
res.writeHead(200, {
'Content-Type': 'text/html;charset=utf-8'
}) const urlObj = url.parse(req.url, true) // true 将参数类型的字符串转成对象 // // write函数不能输出对象,可以输出字符串 ---- 对象转字符串
// res.write(JSON.stringify(urlObj)) // 拿路由
// const pathname = urlObj.pathname
const { pathname } = urlObj; res.write('<h1>您好,世界!</h1>' + pathname); res.end();
}
}); server.listen(3000); console.log('your server is running at http://localhost:3000'); // querystring username=wudaxun&password=123456&sex=1&age=18 转成 对象
3.querystrng将参数类型的字符串转换成对象类型
/**
* querystring
* 将参数类型的字符串转换成对象类型
*
* username=wudaxun&password=123456
*
* ===>
*
* { username: wudaxun, password: 123456 }
*/
const querystring = require('querystring') const queryStr = 'username=wudaxun&password=123456&age=18' // querystring.parse(queryStr) *---- 将字符串类型的参数 转换成 对象 const obj = querystring.parse(queryStr) console.log(obj) /***
* {
username: 'wudaxun',
password: '123456',
age: '18'
} 总结:
实际开发过程中并不需要使用 querystring,建议使用 url.parse(str, true) url.parse()的第二个参数为true可转对象,其原理就是 querystring.parse()
*/ // ??? 已知的网页字符串可以搞定,那么如果是用户输入的网址呢?
4.解析网址(服务器获取的网址)
const http = require('http')
const url = require('url')
const server = http.createServer((req, res) => {
if (req.url !== '/favicon.ico') {
res.writeHead(200, {
'Content-Type': 'text/html;charset=utf-8'
})
const urlStr = req.url // /login?username=wudaxun&password=123456
const obj = url.parse(urlStr, true).query // { username: 'wudaxun', password: '123456' }
const { username, password } = obj
res.write(`用户名:${username}`)
res.write(`密码: ${password}`)
res.end()
}
})
server.listen(3000)
// 登录 注册表单 不要使用 & --- 丢失数据
/**
* 如果用户输入 /home 那么输出 首页
* 如果用户输入 /kind 那么输出 分类
* 如果用户输入 /cart 那么输出 购物车
* 如果用户输入 /user 那么输出 个人中心
*/
fs(file-system)
/**
* fs ---- file system
* 后端语言的标志:
* 文件的读写
*/ // 文件的读操作
// fs.readFile(path, 'utf-8', (err, data) => {}) const fs = require('fs') fs.readFile('./01node介绍.html', 'utf-8', (err, data) => {
if (err) {
console.log(err)
} else {
console.log(data)
}
})
在服务器打开别的网页:
const http = require('http')
const fs = require('fs')
const server = http.createServer((req, res) => {
if (req.url !== '/favicon.ico') {
res.writeHead(200, {
'Content-Type': 'text/html;charset=utf-8'
})
fs.readFile('./12test.html', 'utf-8', (err, data) => {
if (err) throw err // 遇到错误,抛出异常,代码不再继续执行
res.write(data)
res.end()
})
}
})
server.listen(3000)
/**
* 如果用户输入 /home 那么输出 首页.html
* 如果用户输入 /kind 那么输出 分类.html
* 如果用户输入 /cart 那么输出 购物车.html
* 如果用户输入 /user 那么输出 个人中心.html
*/
nodejs,,一些基本操作--server。js的更多相关文章
- NodeJS路由(server.js + router.js)
手动路由... server.js 创建http服务器,解析pathname,传递给router处理. var http = require("http"); var url = ...
- Nodejs入门-基于Node.js的简单应用
服务端JavaScript 众所周知的,JavaScript是运行在浏览器的脚本语言,JavaScript通常作为客户端程序设计语言使用,以JavaScript写出的程序常在用户的浏览器上运行.直至N ...
- Nodejs 操作 Sql Server
Nodejs 操作 Sql Server Intro 最近项目需要爬取一些数据,数据有加密,前端的js又被混淆了,ajax请求被 hook 了,有些复杂,最后打算使用 puppeteer 来爬取数据. ...
- Node聊天程序实例06:server.js
作者:vousiu 出处:http://www.cnblogs.com/vousiu 本实例参考自Mike Cantelon等人的<Node.js in Action>一书. server ...
- nodejs 批处理运行 app.js
1.直接执行run.bat文件 以下的内容为批处理文件run.bat中的内容,批处理命令中NODE_PATH为Node.js的安装路径. 使用express 生成的项目.app.js为 ...
- 解决新版本webpack vue-cli生成文件没有dev.server.js问题
新版本webpack生成的dev.server.js 在webpack.dev.conf.js中 webpack.dev.conf.js const axios = require('axios') ...
- node+express 中安装nodemon实时更新server.js
每次启动node server.js,有一个缺点,每次server.js文件有改动时,必须重新执行指令node server.js,新的代码才会起作用 解决方案1 全局安装 npm install s ...
- nodejs报错 events.js:72 throw er; // Unhandled 'error' event
var http = require('http'); var handlerRequest = function(req,res){ res.end('hello');}; var webServe ...
- 【nodejs】使用Node.js实现REST Client调用REST API
最近在产品中开发基于REST的API接口,结合自己最近对Node.js的研究,想基于它开发一个REST Client做测试之用. 通过初步研究,Node.js开发HTTP Client还是挺方便的. ...
- NodeJs 实时压缩 项目js文件
+ 1. 下载nodejs : "http://nodejs.org/". + 2. 以administrator权限打开cmd.+ 3. cmd路径定位到要压缩的目录: &quo ...
随机推荐
- 选择 podman 的理由, 以及它和 Kubernetes , Docker 的区别
转载自https://zhuanlan.zhihu.com/p/506265757 前言 大家好,我是 Liangdi, podman 4.x 版本已经发布了, 我也从 docker 开始向 podm ...
- Communications link failure:The last packet successfully received from the server was 0 millisecond ago
出现这种错误的大致情况如下: 1.数据库连接长时间未使用,断开连接后,再去连接出现这种情况.这种情况常见于用连接池连接数据库出现的问题 2.数据库连接的后缀参数问题 针对上述两种情况,解决方案如下 1 ...
- Hive+spark工业化项目
DolphinScheduler:国产调度平台 airflow: 调度平台
- k8s集群部署kafka
一.部署步骤 1.部署NFS并挂载共享目录 2.部署zookeeper集群 3.部署kafka集群 4.测试kafka 二.部署NFS并挂载共享目录 注:使用云产品的NAS存储可跳过此步骤 1.服务端 ...
- socket.timeout: The read operation timed out完美解决方案【转】
原博: https://blog.csdn.net/qq_44588905/article/details/113783373 更换 pip 源自国内镜像,在 pip install 后面添加 -i ...
- 8.forEach的使用
1 List<customer> list //一个类是customer的列表 2 3 /* for(int i = 0;i < list.size();i++){ 4 System ...
- Hash中的bucket什么意思?
这个好理解.无序容器的内部是由一个个的bucket(桶)构成的,每个bucket里面由相同hash的元素构成. 因此无序容器的搜索是先根据hash值,定位到bucket,然后再在bucket里面搜索符 ...
- NX二次开发获取NX主程序路径
//头文件 #include <Windows.h> #include <iostream> #include <NXOpen/ListingWindow.hxx> ...
- 安装单机版k8s
1.配置yum源,博主使用华为的镜像源 选择不同的系统版本下载使用: 2.安装etcd,kubernetes yum -y install etcd kubernetes 3.修改kubernetes ...
- Vue2使用axios,request.js和vue.config.js
1.配置request.js,用来请求数据 import axios from 'axios' // 1:利用axios对象的方法create,创建一个axios实例 // 2:request就是ax ...