分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

node.js学习之路(1)

发布时间:2023-09-06 01:24责任编辑:白小东关键词:js

node.js 属于后台语言,后台语言还有php,java等。

优势:1.性能好   node.js VS php   86倍  

           2.跟前台JS配合方便

           3.node.js便于前端学习

https://nodejs.org

Node.js 命令

1.切换盘符 e:

2.改变目录 cd 目录名

3.执行程序 node 文件名.js

Js能用的Node.js 都能用


node.js(自带许多模块可以参考node.js api)

node.js———服务器

http —— 协议

request   请求   输入-请求的信息 

response 响应  输出-输出的信息

搭建简单的服务器:

const http = require(‘http‘); //请求http模块

http.createServer(function(res,req) {
     //createServer(回调函数)用来创建服务器
           //  console.log("有人来了")
       res.write("abc");
       res.end();
     
     //监听//端口
}).listen(8080)
 
 
文件操作:fs — Flie Ssytem
 
同步vs异步
同步—一次一个
异步—多个操作可以同时进行,前一次的操作没完事,后一次也进行操作
 
//读写文件
readFile(文件名,function(err,data){})
writeFile(文件名,内容,function(err,data){})

服务器读写案例

const http = require(‘http‘);
const fs = require(‘fs‘);

http.createServer(function(req, res) {
???//req.url=>‘/index.html‘
???//读取=>‘./www/index.html‘
???//‘./www‘+req.url
???var file_name = ‘./www‘ + req.url;
???fs.readFile(file_name, function(err, data) {
???????if (err) {
???????????res.write(‘404‘)
???????} else {
???????????res.write(data)
???????}
???????res.end();
???});
}).listen(8080)
 
请求方式:
1.get 数据在url中
2.post 数据不再url中
/////////////////////////////////////////////////////////////
req.url =>"/aaa?user=blur&pass=1234"
/aaa
user=blur
pass=1234
//////////////////////////////////////////////////////////////////
GET解析路径
1.自己切
2.querystring模块    xxx=xxxx&xx=xx
3.url模块                 aaa?xxx=xx&xxx=12
 
urlLib=require("url");
urlLib.parse(url,true); pathname
                                   query
POST解析路径
 
POST数据接收:Post 数据比GET大得多
POST 很大 __分段
 
const http = require(‘http‘);
const urlLib = require(‘url‘);
const querystring = require(‘querystring‘);
http.createServer(function(req, res) {
//Post-req
var str = ‘‘; //接受数据
var i = 0;
req.on(‘data‘, function(data) {
console.log(`第${i++}次收到数据`)
str += data;
});
req.on(‘end‘, function() {
var POST = querystring.parse(str);
console.log(POST)
})
}).listen(8088)

 综合案例

const http = require(‘http‘);
const fs = require(‘fs‘);
const urlLib = require(‘url‘);
const querystring = require(‘querystring‘);
http.createServer(function(req, res) {
//GET
const obj = urlLib.parse(req.url, true)

???var url = obj.pathname;
???const GET = obj.query;

???//POST
???var str = ‘‘;
???req.on(‘data‘, function(data) {
???????str += data;
???});
???req.on(‘end‘, function() {
???????????const POST = querystring.parse(str);
???????})
???????/**
????????*
????????*/
???console.log(url, GET, POST)
???????//文件请求

???var flie_name = ‘./www‘ + url;
???fs.readFile(flie_name, function(err, data) {
???????if (err) {
???????????res.write(‘404‘)
???????} else {
???????????res.write(data)
???????}
???????res.end();
???})
}).listen(6666);
 
以上是node的前奏基础知识
后续请继续关注

node.js学习之路(1)

原文地址:http://www.cnblogs.com/dekui/p/7816389.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved