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. Sublime Text3(mac)一些插件和快捷键

    Sublime Text3(mac)一些插件和快捷键 楚简约 关注 2017.02.24 17:02* 字数 1216 阅读 412评论 0喜欢 2 下载地址http://www.sublimetex ...

  2. Apache-Shiro介绍

    Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从最小的移动应用程序到最大的网络 ...

  3. HDU 2235

    这题说的是给了一个 平面 然后又很多的长方体柱子 问这个 容器的 容积是什么, 排序后 然后 进行 并查集 判断是否 可以有比他小的高度依靠他算体积,通过并查集去判断他的子集的个数. #include ...

  4. Python之路----递归函数

    1.小练一下 用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sb name=['alex','wupeiqi','yuanhao','nezha'] # def func(item ...

  5. [转] Oracle学习之创建数据库(新建实例)

    由于项目需求,在本机中开发,需要新建oracle数据库实例,亲测可以. 出处:http://blog.csdn.NET/luiseradl/article/details/6972217 http:/ ...

  6. MySQL Crash Course #12# Chapter 18. Full-Text Searching

    INDEX 由于性能.智能结果等多方面原因,在搜索文本时,全文搜索一般要优于通配符和正则表达式,前者为指定列建立索引,以便快速找到对应行,并且将结果集智能排序.启用查询扩展可以让我们得到未必包含关键字 ...

  7. mysql与oracle常用函数及数据类型对比00持续补充

    最近在转一个原来使用oracle,改为mysql的系统,有些常用的oracle函数的mysql实现顺便整理了下,主要是系统中涉及到的(其实原来是专门整理过一个详细doc的,只是每次找word麻烦). ...

  8. 06: linux下python开发环境梳理

    1.1 修改~/.bashrc文件 改编终端颜色 alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # User specific aliases ...

  9. 20145127《java程序设计》第二次实验

    一.实验内容及其步骤 1.要想对某个程序进行单元测试,我们先是在eclipse中建立了一个新的项目,项目的名字是TDDDmeo.并在这个新的项目里右键单击创建一个source floder.并将flo ...

  10. Python3基础 list str转成list

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...