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 服务器的更多相关文章

  1. Node.js 创建server服务器

    var http=require('http'); //引入http模块 var server=http.createServer(function(req,res){  //创建一个server r ...

  2. Office Web Apps Server 概述

    Office Web Apps Server 是新的 Office 服务器产品,它提供 Word.PowerPoint.Excel 和 OneNote 的基于浏览器的版本.单个 Office Web ...

  3. [转载]部署Office Web Apps Server并配置其与SharePoint 2013的集成

    Office Web Apps Server 是新的 Office 服务器产品,它提供 Word.PowerPoint.Excel 和 OneNote 的基于浏览器的版本.单个 Office Web ...

  4. 部署Office Web Apps Server并配置其与SharePoint 2013的集成

    部署Office Web Apps Server并配置其与SharePoint 2013的集成   Office Web Apps Server 是新的 Office 服务器产品,它提供 Word.P ...

  5. Office Web Apps Server

    Office Web Apps Server Office Web Apps Server 是一款 Office 服务器产品,可提供针对 Office 文件的基于浏览器的文件查看和编辑服务.Offic ...

  6. office web apps server安装部署

    操作系统:windows 2012 软件下载地址: 链接:https://pan.baidu.com/s/1c3WWFs8 密码:4dcy NDP452-KB2901954-Web.exe(.Net ...

  7. Windows Server 2012 R2安装部署Office Web Apps Server

    微软官方参考地址https://technet.microsoft.com/zh-cn/library/jj219455.aspx,建议参考官方说明. 注意:每一步进行完成后重启服务器!!! 一.   ...

  8. web server服务器

    使用最多的 web server服务器软件有两个:微软的信息服务器(iis),和Apache. 通俗的讲,Web服务器传送(serves)页面使浏览器可以浏览,然而应用程序服务器提供的是客户端应用程序 ...

  9. Node.js 创建HTTP服务器

    Node.js 创建HTTP服务器 如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个& ...

  10. Node.js 创建HTTP服务器(经过测试,这篇文章是靠谱的T_T)

    Node.js 创建HTTP服务器 如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个& ...

随机推荐

  1. mysql外键使用和事物使用

    mysql外键功能主要是为了保证关联表数据的一致性,主要目的是控制存储在外键表中的数据. 使两张表形成关联,外键只能引用外表中的列的值! 例如: a b 两个表 a表中存有 客户号,客户名称 b表中存 ...

  2. #C++初学记录(深度搜索#递归)

    深度搜索 走地图的题目是深度搜索里比较容易理解的题目,更深层次的是全排列和七皇后等经典题目,更加难以理解,代码比较抽象. 题目:红与黑 蒜厂有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖. ...

  3. SV搭建验证环境

    1)首先定义纯虚类Sv_object,主要实现下边两个function: 定义local static 变量nextobjectID; 虚方法 virtual function void copy(S ...

  4. NOSQL学习之一:Memcached, Redis, MongoDB区别

    Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理. Memcached是一个自由开源的,高性能,分布式内存对象缓存系统. MongoDB是一个基于分布 ...

  5. OLAP引擎——Kylin介绍(很有用)

    转:http://blog.csdn.net/yu616568/article/details/48103415 Kylin是ebay开发的一套OLAP系统,与Mondrian不同的是,它是一个MOL ...

  6. vc++之stdafx.h

    关于stdafx.h的解释,其实蛮多的,在vs中,既然创建c++工程的时候,默认会给生成main.cpp,并且自动包含了stdafx.h,而且stdafx.h不是c++标准的一部分,那么个人认为,理解 ...

  7. 20145122《 Java网络编程》实验五实验报告

    实验名称 Java网络编程 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 结对小伙伴 20145120黄玄曦 博客地址:http://www.cnblo ...

  8. Python字典猜解

    摘要 目标 使用Python破解WordPress用户密码 使用Python破解zip压缩包密码 思路 通过表单提交项构建数据包,使用字典中的可选字符进行逐一排列组合暴力破解WordPress的用户密 ...

  9. 20165310 学习基础和C语言基础调查

    学习基础和C语言基础调查 做中学体会 阅读做中学之后,了解老师关于五笔练习.减肥.乒乓和背单词的经历,不禁联想到自己学古筝的经历. 成功的经验 兴趣 我其实小时候学过一段时间古筝,但是那时候是因为父母 ...

  10. Win32程序支持命令行参数的做法(转载)

    转载:http://www.cnblogs.com/lanzhi/p/6470406.html 转载:http://blog.csdn.net/kelsel/article/details/52759 ...