`
zsjg13
  • 浏览: 142541 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
社区版块
存档分类
最新评论

Your First Web Server

阅读更多

    现在,你已准备好编写有点有趣的东西了,就比如我们要写一个小的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 就行了。它会很聪明地清掉所有东西并正确关闭。

分享到:
评论

相关推荐

    Get Programming with Node.js 1st Edition.pdf

    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 ...

    Getting Started with Varnish Cache: Accelerate Your Web Applications

    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 javaee platform.pdf

    《Your First Cup: An Introduction to the Java EE Platform》是一本由Oracle公司出版的技术手册,旨在为初学者介绍Java Platform, Enterprise Edition(Java EE)的基础知识。该书出版于2011年6月,提供了关于Java...

    Node.js Web Development

    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 EE6 Platform.pdf

    《Your First Cup: An Introduction to the Java EE Platform》是一本由Sun Microsystems出版的专业书籍,主要介绍了Java EE 6平台的基本概念、特点以及如何利用该平台进行企业级应用开发。本书作为Java EE初学者的...

    Single Page Web Applications JavaScript end-to-end

    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.Automating.Tasks.on.the.Web.pdf

    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 ...

    Go Web Development Cookbook2018

    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 ...

    WebDynpro Tutorials from SAP Part 1/3

    1. 创建你的第一个Web Dynpro应用(01 Creating Your First Web Dynpro App): 这一章介绍了如何从零开始创建一个Web Dynpro应用。包括定义应用元数据、设置视图、创建控制器以及将它们连接起来。这通常是学习任何...

    EFcore连接本地Mysql数据库使用ASP.NET.CORE.API

    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 ...

    使用Burpsuite精通Web渗透测试-英文版 非常好

    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 ...

    Programming.Microsoft.SQL.Server.2000.with.XML

    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....

    WEB发布初学者开发

    5. 创建JSP页面:JSP(Java Server Pages)页面是另一种用于构建WEB应用的文件,它允许在HTML中嵌入Java代码。JSP页面能够自动生成HTML,非常方便用于动态内容的展示。 6. 在编辑器中显示行号:这是一个开发过程中...

    MEAN.Web.Development.2nd.Edition.epub

    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 ...

    Learning.Web.Development.with.Bootstrap.and.Angular.2nd.Edition.epub

    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 ...

Global site tag (gtag.js) - Google Analytics