node - web 服务器 、server 服务器
node 作为服务端 - 基本使用
1. web 服务器
web.html
<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
div{
border: 1px solid red;
}
</style>
</head> <body>
<h1>我的第一个标题</h1>
<p>我的第一个段落。</p>
<div>content</div>
<img src="https://www.baidu.com/img/bd_logo1.png">
<iframe src="https://www.baidu.com/"></iframe>
</body> </html>
http.js
var http = require('http');
var fs = require('fs');
var url = require('url');
http.createServer(function (request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
console.log(err);
response.writeHead(404, {
'Content-Type': 'text/html'
});
} else {
response.writeHead(200, {
'Content-Type': 'text/html'
});
response.write(data.toString());
}
response.end();
});
}).listen(5555);
console.log('Server running at http://127.0.0.1:5555/');
访问: http://127.0.0.1:5555/web.html
细节点:
如果是 cmd 后,这个运行方式, C:\Users\admin>node e:\nodeTest\nodeFile\http ,会错误提示: Error: ENOENT: no such file or directory, open 'C:\Users\admin\web.html'
切换到 server 端 js 根目录运行即可 : cd... ,最后这样子: e:\nodeTest\nodeFile>node http
如果设置返回结果字符串允许带script之类的标签,则容易形成 xss攻击 ,如 '<script>alert("hello")</script>'
2. server 服务端
web.html
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8" />
<title>add human</title>
</head> <body>
<div class="radio">
<label>
<input type="radio" name="human" id="man" value="男" checked>男
</label>
<label>
<input type="radio" name="human" id="woman" value="女" checked>女
</label>
</div>
<button id="btn" type="button" >点击保存</button>
<div style="border:1px solid red" id="wrap"></div> <script src="jquery-1.9.1.min.js"></script>
<script>
$('#btn').on('click', function () {
$.ajax({
url: 'http://localhost:3000/addMan',
type: 'get',
dataType: 'json',
data: {
name: $('#man').val()
},
success: function (data) {
console.log(data);
$('#wrap').html(data);
}
});
});
</script>
</body> </html>
server.js
var http = require('http');
var querystring = require('querystring');
var app = http.createServer();
app.on('request', function (req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
//res.setHeader('Access-Control-Allow-Origin', 'http://localhost');
if (req.url === '/addMan' && req.method === 'POST') {
var data = '';
req.on('data', function (chunk) {
data += chunk;
});
req.on('end', function () {
data = decodeURI(data);
var dataObject = querystring.parse(data);
console.log(dataObject);
});
}
res.end(JSON.stringify('wasd'+Math.random()));
});
app.listen(3000, function () {
console.log('add man');
});
暂时不考虑数据库,同时启动 server.js 和 http.js 、web.html 即形成常见表单提交应用
node - web 服务器 、server 服务器的更多相关文章
- Node.js 创建server服务器
var http=require('http'); //引入http模块 var server=http.createServer(function(req,res){ //创建一个server r ...
- Office Web Apps Server 概述
Office Web Apps Server 是新的 Office 服务器产品,它提供 Word.PowerPoint.Excel 和 OneNote 的基于浏览器的版本.单个 Office Web ...
- [转载]部署Office Web Apps Server并配置其与SharePoint 2013的集成
Office Web Apps Server 是新的 Office 服务器产品,它提供 Word.PowerPoint.Excel 和 OneNote 的基于浏览器的版本.单个 Office Web ...
- 部署Office Web Apps Server并配置其与SharePoint 2013的集成
部署Office Web Apps Server并配置其与SharePoint 2013的集成 Office Web Apps Server 是新的 Office 服务器产品,它提供 Word.P ...
- Office Web Apps Server
Office Web Apps Server Office Web Apps Server 是一款 Office 服务器产品,可提供针对 Office 文件的基于浏览器的文件查看和编辑服务.Offic ...
- office web apps server安装部署
操作系统:windows 2012 软件下载地址: 链接:https://pan.baidu.com/s/1c3WWFs8 密码:4dcy NDP452-KB2901954-Web.exe(.Net ...
- Windows Server 2012 R2安装部署Office Web Apps Server
微软官方参考地址https://technet.microsoft.com/zh-cn/library/jj219455.aspx,建议参考官方说明. 注意:每一步进行完成后重启服务器!!! 一. ...
- web server服务器
使用最多的 web server服务器软件有两个:微软的信息服务器(iis),和Apache. 通俗的讲,Web服务器传送(serves)页面使浏览器可以浏览,然而应用程序服务器提供的是客户端应用程序 ...
- Node.js 创建HTTP服务器
Node.js 创建HTTP服务器 如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个& ...
- Node.js 创建HTTP服务器(经过测试,这篇文章是靠谱的T_T)
Node.js 创建HTTP服务器 如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个& ...
随机推荐
- testng入门教程1在testng运行一个简单的testcase
在eclips运行java,创建一个Java类文件名TestNGSimpleTest C:\ > TestNG_WORKSPACE import org.testng.annotations. ...
- mysql数据库环境配置中部分问题解决办法
注:原文地址:https://www.cnblogs.com/hezhuoheng/p/9366630.html 其中最重要的,是三个原则:命令按顺序输入.删除了ini(这个不是原则,是我解决问题的一 ...
- FAFU 1395
动态规划:...翻牌FAFU 1395 动态规划
- 并发写Btree原理剖析
OceanBase 0.4的UpdateServer存储引擎使用了一棵可以多线程并发修改的BTree,读写不冲突,由颜然 开发.线上运行稳定. UpdateServer存储引擎采用类leveldb的方 ...
- JS重要的内置对象
Array对象: 属性: .length 获得数组的长度: 方法: .concat() 连接内容或者数组,组成新的数组: .join(n) 用n连接数组的每一项组成字符串,可以是空字符串: ...
- 虚拟机中安装mac系统
虚拟机安装就很简单了,傻瓜式安装,一直点击下一步就行,这里就不多说了. 所需要的配置: 虚拟机下载地址:链接:http://pan.baidu.com/s/1i45wXRf 密码:7c4x mac补丁 ...
- Hadoop MapReduce执行过程实例分析
1.MapReduce是如何执行任务的?2.Mapper任务是怎样的一个过程?3.Reduce是如何执行任务的?4.键值对是如何编号的?5.实例,如何计算没见最高气温? 分析MapReduce执行过程 ...
- 02: DOM 实例
1.1 Event 对象 <body> <a id="myAnchor" href="http://www.microsoft.com"> ...
- Python3基础 file open 打开txt文件并打印出全文
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- hdu4719 Oh My Holy FFF 线段树优化dp
思路 好久之前的了,忘记什么题目了 可以到我这里做luogu 反正就是hdu数据太水,导致自己造的数据都过不去,而hdu却A了 好像是维护了最大值和次大值,然后出错的几率就小了很多也许是自己写错了,忘 ...