[ 重 新 预 习 ] Node.js搭建服务
中级实作学的东西。作业做的是一个电影推荐网站,负责的部分是后台管理页面。已经忘记怎么做的了。
服务器端
使用Nodejs + http 创建web服务器
1.导入模块
2.创建服务器,设置监听端口
3.找到文件路径
4.读取文件到前端
5.结束请求
#server.js
//使用Nodejs + http 创建web服务器var http = require("http"); //http模块,用来创建服务器
var fs = require("fs"); //fs模块文件系统,读取文件写到前端
var url = require("url"); //解析网址路径模块,找到文件位置//配置一个服务器
http.createServer(function (request, response) {var pathname = url.parse(request.url).pathname;//parse显示已被废弃console.log(url.parse(request.url))/*Url {protocol: null,slashes: null,auth: null,host: null,port: null,hostname: null,hash: null,search: null,query: null,pathname: '/server.html',path: '/server.html',href: '/server.html'ANDpathname: '/test.css',path: '/test.css',href: '/test.css'}*/console.log("Request for " + pathname + " received.");//Request for /server.html received.//Request for /test.css received.var firstDir = pathname && pathname.split('.')[1]; //文件类型 htmlvar ContentType = {'Content-Type': 'text/html'};if (firstDir == 'css') {ContentType = {'Content-Type': 'text/css'};}fs.readFile(pathname.substr(1), function (err, data) {//substr显示已被废弃console.log(pathname.substr(1)) //test/test.htmlif (err) {console.log(err);//HTTP 状态码 404 : NOT FOUND//Content Type:text/plainresponse.writeHead(404, {'Content-Type': 'text/html'});} else {//HTTP 状态码 200 : OK//Content Type:text/plainresponse.writeHead(200, ContentType);//写会回相应内容response.write(data.toString());}//发送响应数据,结束请求,否则前端会一直处于等待状态response.end();});}).listen(8081); //监听端口
console.log('Server running at http://127.0.0.1:8081/');
谢谢博主勤奋者是天才Nodejs搭建web服务器_技术宅男-CSDN博客_nodejs搭建web服务器,在其分享的代码基础上,我补充了一些自己不熟悉的点。
#server.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><link rel="stylesheet" href="/test.css">
</head><body><h1 class="hello">hello!</h1>
</body></html>
#test.css
h1{color: red;
}
结果
substr和parse显示已被废弃,还没做了解。
解析一下其他链接试试看。
console.log(url.parse("https://blog.csdn.net/weixin_46106424?type=blog"))/*Url {protocol: 'https:',slashes: true,auth: null,host: 'blog.csdn.net',port: null,hostname: 'blog.csdn.net',hash: null,search: null,query: null,pathname: '/XIAO_YuBaby/article/details/103190255',path: '/XIAO_YuBaby/article/details/103190255',href: 'https://blog.csdn.net/XIAO_YuBaby/article/details/103190255'}*/
使用Nodejs + express创建web服务器
——————被MaskRCNN截胡了,不想放在草稿箱,先发,晚点再继续更新———————
[ 重 新 预 习 ] Node.js搭建服务相关推荐
- js word 预览_Node.js微服务实践(二)
基于Seneca 和 PM2构建 本章主要分为三个小节: 选择Nodejs的理由:将证明选择Node.js来构建的正确性.介绍使用Node.js时设计的软件栈. 微服务架构Seneca:关于Senec ...
- mysql第五章项目二_Todo List:Node+Express 搭建服务端毗邻Mysql – 第五章(第1节)
点击右上方红色按钮关注"web秀",让你真正秀起来 前言 万丈高楼平地起,我们的Todo List项目也是越来越结实了.Todo List的前面4章内容都是在为Client端开发, ...
- 一、node.js搭建最简单的服务器
node.js搭建最简单的服务器 代码演示: // 1. 加载http核心模块 var http = require('http')// 2. 使用http.createServer()方法创建一个W ...
- 高质量 Node.js 微服务的编写和部署
前几天在微信群做的一次分享,整理出来分享给大家,相关代码请戳 https://github.com/Carrotzpc/docker_web_app 微服务架构是一种构造应用程序的替代性方法.应用程序 ...
- 个推Node.js 微服务实践:基于容器的一站式命令行工具链
2019独角兽企业重金招聘Python工程师标准>>> 作者:个推Node.js 开发工程师 之诺 背景与摘要 由于工程数量的快速增长,个推在实践基于 Node.js 的微服务开发的 ...
- Node.js 微服务实践:基于容器的一站式命令行工具链
作者:个推Node.js 开发工程师 之诺 背景与摘要 由于工程数量的快速增长,个推在实践基于 Node.js 的微服务开发的过程中,遇到了如下问题: 每次新建项目都需要安装一次依赖,这些依赖之间基本 ...
- Node.js 微服务实践:基于容器的一站式命令行工具链...
作者:个推Node.js 开发工程师 之诺 背景与摘要 由于工程数量的快速增长,个推在实践基于 Node.js 的微服务开发的过程中,遇到了如下问题: 每次新建项目都需要安装一次依赖,这些依赖之间基本 ...
- Node.js(一)——(Node.js安装及使用,通过Node.js搭建服务器,模块化及自定义模块,npm/yarn/nvm,内置模块fs的使用,buffer及stream,新闻列表案例)
目录 1.Node.js介绍 2.安装Node.js 3.使用Node.js实现第一个服务器 3.1初步感受Node.js 3.2Google Chrome 默认非安全端口列表,尽量避免以下端口. 3 ...
- node.js搭建简易Web服务器
node.js搭建简易Web服务器 node.js简介 Node.js 是一个基于V8引擎的JavaScript 运行环境. V8 是为Google Chrome 提供支持的 JavaScript 引 ...
最新文章
- Spark分析之Standalone运行过程分析
- 两个数之和等于第三个数
- SAP Commerce Extension的Web应用启动问题
- 栈和队列都是限制存取点的线性结构_栈的练习以及解析
- oracle跨越千年处理
- 小程序 const moment = require('moment')_开源小程序精选
- 09-Windows Server 2012 R2 会话远程桌面-标准部署-使用PowerShell进行部署2-2
- CPU实时人脸检测,各种朝向、侧脸都检出来
- 剧情介绍:“造雨人”
- SpringBoot+Quartz实现动态可配定时任务(动态定时任务)
- 再见,可视化!你好,Pandas!
- html左侧菜单展开与收起,CSS3 实现侧边栏展开收起动画
- python语言的实验心得体会范文_实验报告心得体会范文3篇_心得体会
- js-xlsx 读取Excel解析
- tekton pipeline资源
- [BZOJ]1064 [NOI2008] 假面舞会 dfs判环
- 《PTA——拼题A》之第1008题
- 如何设计一个开放平台openapi?
- CSS3 HTML5下雪特效 雪花飘飘
- 浅谈Intel QPI的MESIF协议和Home,Source Snoop