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. HOW TO: 在 Visual C# .NET 应用程序中提供文件拖放功能

    本文假定您熟悉下列主题: Windows 窗体列表框控件 Windows 窗体事件处理 生成示例的步骤 列表框控件提供了您需要处理的两个拖放事件: DragEnter 和 DragDrop. 当您在控 ...

  2. acrobat pro 无法编辑个别文本

    在修改pdf文档时出现个别文字选取不上,无法修改,如图中4-2626没有选中 解决方法如图 此后可以直接修改文本了

  3. linux常用命令:less 命令

    less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...

  4. python socket编程函数介绍

    网上看到一个socket中常用函数的介绍,记录一下 https://blog.csdn.net/rebelqsp/article/details/22109925

  5. mysql数据库----索引原理与慢查询优化

    一.介绍 1.什么是索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语 ...

  6. Web负载均衡学习笔记之四层和七层负载均衡的区别

    0x00 简介 简单理解四层和七层负载均衡: ① 所谓四层就是基于IP+端口的负载均衡:七层就是基于URL等应用层信息的负载均衡:同理,还有基于MAC地址的二层负载均衡和基于IP地址的三层负载均衡. ...

  7. finedb(内置的HSQL数据库)迁移数据到MySQL

    finedb(内置的HSQL数据库)迁移数据到MySQL 1. 前言 在FineBI中,决策平台的数据(用户.角色.组织机构.权限等信息)是存储在finedb数据库中的,默认情况下finedb是一个内 ...

  8. tomcat 7下spring 4.x mvc集成websocket以及sockjs完全参考指南(含nginx/https支持)

    之所以sockjs会存在,说得不好听点,就是因为微软是个流氓,现在使用windows 7的系统仍然有近半,而windows 7默认自带的是ie 8,有些会自动更新到ie 9,但是大部分非IT用户其实都 ...

  9. 03: MySQL基本操作

    MySQL其他篇 目录: 参考网站 1.1 MySQL 三种数据类型(数值,字符串,日期) 1.2 MySQL常用增删改查命令 1.3 删除,添加或修改表字段 1.4 MySQL外键关联(一对多) 1 ...

  10. Leading and Trailing(数论/n^k的前三位)题解

    Leading and Trailing You are given two integers: n and k, your task is to find the most significant ...