使用node.js作为简单的Web服务器 [英] Using node.js as a simple web server

查看:127
本文介绍了使用node.js作为简单的Web服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个非常简单的HTTP服务器.每个对example.com的GET请求都应将其作为常规HTML页面(例如,与您阅读普通网页时相同的体验)提供给index.html.

I want to run a very simple HTTP server. Every GET request to example.com should get index.html served to it but as a regular HTML page (i.e., same experience as when you read normal web pages).

使用下面的代码,我可以读取index.html的内容.如何将index.html用作常规网页?

Using the code below, I can read the content of index.html. How do I serve index.html as a regular web page?

var http = require('http');
var fs = require('fs');
var index = fs.readFileSync('index.html');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end(index);
}).listen(9615);


下面的一个建议很复杂,要求我为要使用的每个资源(CSS,JavaScript,图像)文件写一个get行.


One suggestion below is complicated and requires me to write a get line for each resource (CSS, JavaScript, images) file I want to use.

如何在单个HTML页面中提供一些图像,CSS和JavaScript?

How can I serve a single HTML page with some images, CSS and JavaScript?

推荐答案

最简单的Node.js服务器就是:

Simplest Node.js server is just:

$ npm install http-server -g

现在您可以通过以下命令运行服务器:

Now you can run a server via the following commands:

$ cd MyApp

$ http-server


如果您使用的是NPM 5.2.0或更高版本,则可以使用http-server而不将其与npx一起安装.不建议在生产中使用此方法,但这是快速使服务器在localhost上运行的好方法.


If you're using NPM 5.2.0 or newer, you can use http-server without installing it with npx. This isn't recommended for use in production but is a great way to quickly get a server running on localhost.

$ npx http-server


或者,您可以尝试执行此操作,这将打开Web浏览器并启用CORS请求:


Or, you can try this, which opens your web browser and enables CORS requests:

$ http-server -o --cors

有关更多选项,请查看GitHub上http-server文档,或运行:

For more options, check out the documentation for http-server on GitHub, or run:

$ http-server --help

许多其他不错的功能以及可以简单地部署到NodeJitsu的功能.

Lots of other nice features and brain-dead-simple deployment to NodeJitsu.

功能叉

当然,您可以使用自己的fork轻松添加功能.您可能会发现它已经在该项目的现有800多个分支中完成:

Of course, you can easily top up the features with your own fork. You might find it's already been done in one of the existing 800+ forks of this project:

Light Server:一种自动刷新的替代方法

http-server的一个很好的替代方法是 light-server .它支持文件监视和自动刷新以及许多其他功能.

A nice alternative to http-server is light-server. It supports file watching and auto-refreshing and many other features.

$ npm install -g light-server 
$ light-server

在Windows资源管理器中添加到目录上下文菜单

 reg.exe add HKCR\Directory\shell\LightServer\command /ve /t REG_EXPAND_SZ /f /d "\"C:\nodejs\light-server.cmd\" \"-o\" \"-s\" \"%V\""

简单的JSON REST服务器

如果您需要为原型项目创建简单的REST服务器,则 json-服务器就是您要寻找的东西.

If you need to create a simple REST server for a prototype project then json-server might be what you're looking for.

自动刷新编辑器

大多数网页编辑器和IDE工具现在都包括一个Web服务器,该服务器将监视您的源文件并在它们更改时自动刷新您的网页.

Most web page editors and IDE tools now include a web server that will watch your source files and auto refresh your web page when they change.

我将 Live Server 与Visual Studio代码一起使用.

I use Live Server with Visual Studio Code.

开源文本编辑器支架还包括一个NodeJS静态Web服务器.只需在Brackets中打开任何HTML文件,然后按" Live Preview ",它就会启动静态服务器并在该页面上打开浏览器.只要您编辑和保存HTML文件,浏览器就会自动刷新.这在测试自适应网站时特别有用.在多个浏览器/窗口大小/设备上打开HTML页面.保存您的HTML页面,并立即查看您的自适应内容是否在自动刷新时正常工作.

The open source text editor Brackets also includes a NodeJS static web server. Just open any HTML file in Brackets, press "Live Preview" and it starts a static server and opens your browser at the page. The browser will **auto refresh whenever you edit and save the HTML file. This especially useful when testing adaptive web sites. Open your HTML page on multiple browsers/window sizes/devices. Save your HTML page and instantly see if your adaptive stuff is working as they all auto refresh.

PhoneGap开发人员

如果您要编写混合移动应用,则可能有兴趣了解 PhoneGap 团队通过其新的

If you're coding a hybrid mobile app, you may be interested to know that the PhoneGap team took this auto refresh concept on board with their new PhoneGap App. This is a generic mobile app that can load the HTML5 files from a server during development. This is a very slick trick since now you can skip the slow compile/deploy steps in your development cycle for hybrid mobile apps if you're changing JS/CSS/HTML files — which is what you're doing most of the time. They also provide the static NodeJS web server (run phonegap serve) that detects file changes.

PhoneGap + Sencha Touch开发人员

我现在已经广泛采用了PhoneGap静态服务器&适用于Sencha Touch&的PhoneGap开发人员应用程序jQuery Mobile开发人员.在 Sencha Touch Live 中进行检查.支持--qr QR代码和--localtunnel,可将您的静态服务器从台式机代理到防火墙之外的URL!大量使用.混合移动开发人员的大规模提速.

I've now extensively adapted the PhoneGap static server & PhoneGap Developer App for Sencha Touch & jQuery Mobile developers. Check it out at Sencha Touch Live. Supports --qr QR Codes and --localtunnel that proxies your static server from your desktop computer to a URL outside your firewall! Tons of uses. Massive speedup for hybrid mobile devs.

Cordova + Ionic框架开发人员

本地服务器和自动刷新功能已包含在ionic工具中.只需从您的应用程序文件夹中运行ionic serve.甚至更好... ionic serve --lab 可以查看两个iOS的自动刷新并排视图和Android.

Local server and auto refresh features are baked into the ionic tool. Just run ionic serve from your app folder. Even better ... ionic serve --lab to view auto-refreshing side by side views of both iOS and Android.

这篇关于使用node.js作为简单的Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆