现在,你已准备好编写有点有趣的东西了,就比如我们要写一个小的web server。幸运的是,Node 让这样的任务变得很简单。把下面的代码保存到一个叫 web.js 的文件中:
var http = require("http");
function process_request(req, res) {
var body = 'Thanks for calling!\n';
var content_length = body.length;
res.writeHead(200, {
'Content-Length': content_length,
'Content-Type': 'text/plain'
});
res.end(body);
}
var s = http.createServer(process_request);
s.listen(8080);
要运行该代码,只需输入 node web.js
现在,你的计算机上就有了一个 web server 运行在 8080 端口上。要测试它,你可以使用命令行程序 curl,大多数 Mac 以及 Linux 机器上已经装了。当然也可以在 浏览器 中输入 http://localhost:8080 来检验。
curl -i http://localhost:8080
你可以看到类似于下面的输出:
HTTP/1.1 200 OK
Content-Length: 20
Content-Type: text/plain
Date: Tue, 15 Feb 2013 03:05:08 GMT
Connection: keep-alive
Thanks for calling!
【Downloading from the Web on Windows】
=============================================================================
默认,Windows 没有带任何可以得到一个 URL 的内容的命令行工具。这工具实在不错,我建议你为Windows 下载 cURL 或 wget。
【Curl】:到http://curl.haxx.se/download.html 去下载 Windows binaries for curl,在页面中找到 Win32-Generic 部分。最好下载带 SSL和SSH的。解压后,把 curl.exe 的路径放到 PATH 环境变量中。
C:\Users\Mark\curl --help
【Wget】:它工作起来基本和 crul 一样,但它带了一些稍微不同的命令行参数。看它的帮助能学到更多:
C:\Users\Mark\wget --help
=============================================================================
前面代码中的 require 函数的作用是把一个模块包含进来。createServer 函数带一个参数,该参数是一个函数,每当一个人连接到你的服务器,该函数就会被调用。
-i 开关是告诉 curl 打印 headers 和 response。Node.js,默认到它的服务器的 HTTP 连接是 keep-alive,这样就允许在同一个网络连接上可以有多个请求。
要停止该服务器,按下 Ctrl+C 就行了。它会很聪明地清掉所有东西并正确关闭。
相关推荐
In this engaging tutorial, you'll work through eight complete projects, from writing the code for your first web server to adding live chat to a web app. Your hands will stay on the keyboard as you ...
Varnish mimicks the behavior of your webserver, caches its output in memory, and serves the result directly to clients without having to access your webserver. If you’re a web developer familiar with...
《Your First Cup: An Introduction to the Java EE Platform》是一本由Oracle公司出版的技术手册,旨在为初学者介绍Java Platform, Enterprise Edition(Java EE)的基础知识。该书出版于2011年6月,提供了关于Java...
You will progress from a rudimentary knowledge of JavaScript and server-side development to being able to create, maintain, deploy and test your own Node.js application.You will understand the ...
《Your First Cup: An Introduction to the Java EE Platform》是一本由Sun Microsystems出版的专业书籍,主要介绍了Java EE 6平台的基本概念、特点以及如何利用该平台进行企业级应用开发。本书作为Java EE初学者的...
Single page web applications are your next step: pushing UI rendering and business logic to the browser and communicating with the server only to synchronize data, they provide a smooth user ...
Web Client Programming with Perl is a behind-the-scenes look at how your web browser interacts with web servers. Readers of this book will learn how the Web works and how to write software that is ...
Chapter 1, Creating Your First Server in Go, explains how to write and interact with HTTP and TCP servers, optimize server responses with GZIP compression, and implement routing and logging in a Go ...
Burp Suite is a Java-based platform used for testing the security of your web applications, and has been adopted widely by professional enterprise testers. The Burp Suite Cookbook contains recipes to ...
1. 创建你的第一个Web Dynpro应用(01 Creating Your First Web Dynpro App): 这一章介绍了如何从零开始创建一个Web Dynpro应用。包括定义应用元数据、设置视图、创建控制器以及将它们连接起来。这通常是学习任何...
2.最新版本的ASP.NET.CORE.Web API 3.实现dbfrist和code 。 4.博客链接:https://blog.csdn.net/EAyayaya/article/details/124048491 5. 不是MVC方式 6.Entity Framework Core Entity Framework Core.Tools Entity ...
By adding support for XML to Microsoft SQL Server 2000, Microsoft has made it easier than ever to integrate SQL Server–based solutions with existing systems, Web applications, and trading partners....
5. 创建JSP页面:JSP(Java Server Pages)页面是另一种用于构建WEB应用的文件,它允许在HTML中嵌入Java代码。JSP页面能够自动生成HTML,非常方便用于动态内容的展示。 6. 在编辑器中显示行号:这是一个开发过程中...
Use Socket.io to create real-time communication between your client and server Test your application's Express and Angular 2 entities About the Author Amos Q. Haviv is a software developer, technical ...
You'll dive, fingers first, into the basics of both the tools,and once you're familiar with them, you'll get your hands dirty by working with Bootstrap's new Grid system and Angular's built-in ...