Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 我可以将节点用作html应用程序的Web服务器吗?_Node.js - Fatal编程技术网

Node.js 我可以将节点用作html应用程序的Web服务器吗?

Node.js 我可以将节点用作html应用程序的Web服务器吗?,node.js,Node.js,很抱歉问这么简单的问题。有人向我发送了一些文件,一个index.html文件,它在script标记中拉入一个js文件。我必须启动一个Web服务器才能通过身份验证并查看文件(在dev中是am) 在我的CLI中,我已导航到包含index.html的目录。我已经通过node-v检查了它是否已在全球范围内安装(是的,V8.6)。我已经运行了简单的命令节点,并在和其他几个端口检查了浏览器,但没有得到任何乐趣。我还尝试了node index.html,但CLI抛出了一个错误 如何启动Web服务器?所有在线示

很抱歉问这么简单的问题。有人向我发送了一些文件,一个index.html文件,它在
script
标记中拉入一个js文件。我必须启动一个Web服务器才能通过身份验证并查看文件(在dev中是am)

在我的CLI中,我已导航到包含index.html的目录。我已经通过
node-v
检查了它是否已在全球范围内安装(是的,V8.6)。我已经运行了简单的命令
节点
,并在和其他几个端口检查了浏览器,但没有得到任何乐趣。我还尝试了
node index.html
,但CLI抛出了一个错误

如何启动Web服务器?所有在线示例都告诉我构建一个.js文件,但这不是一个选项

是的,这是可能的

一个非常简单的例子就是创建一个文件,我们称它为
app.js
,并将其放入其中:

const http = require('http'),   // to listen to http requests
      fs = require('fs');       // to read from the filesystem

const app = http.createServer((req,res) => {
    // status should be 'ok'
    res.writeHead(200); 

    // read index.html from the filesystem, 
    // and return in the body of the response
    res.end(fs.readFileSync("index.html")); 
});

app.listen(3000); // listen on 3000
现在,运行
node app.js

浏览至
http://localhost:3000

有很多其他npm包可以帮助您完成这项工作,但这是最简单的“纯节点”示例,可以直接读取index.html并将其作为响应返回。

是的,这是可能的

一个非常简单的例子就是创建一个文件,我们称它为
app.js
,并将其放入其中:

const http = require('http'),   // to listen to http requests
      fs = require('fs');       // to read from the filesystem

const app = http.createServer((req,res) => {
    // status should be 'ok'
    res.writeHead(200); 

    // read index.html from the filesystem, 
    // and return in the body of the response
    res.end(fs.readFileSync("index.html")); 
});

app.listen(3000); // listen on 3000
现在,运行
node app.js

浏览至
http://localhost:3000


有很多其他npm包可以帮助您完成这项工作,但这是最简单的“纯节点”示例,可以直接读取index.html并将其作为响应返回。

因为您不想构建后端,而只想构建http服务器。 我建议使用一个npm包来满足您的需求:

打开控制台

npm安装http服务器-g

转到“index.html”文件夹(在控制台中),然后键入:

http服务器

然后,在浏览器中以以下地址访问内容:

http://localhost:8080

此处的文档:
因为您不想构建后端,而只想构建一个http服务器。 我建议使用一个npm包来满足您的需求:

打开控制台

npm安装http服务器-g

转到“index.html”文件夹(在控制台中),然后键入:

http服务器

然后在浏览器中的以下地址访问您的内容:

http://localhost:8080

此处的文档:

使用node js启动服务器非常容易

var express = require('express');
var app = express();

app.use(express.static(__dirname + '/www'));

app.listen('3000');
console.log('working on 3000');
创建一个
server.js
文件

const http = require('http')
const fs = require('fs');

http.createServer(function (req, res) {
  fs.readFile('index.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    res.end();
  });
}).listen(3000);
运行
node server.js


这甚至可以解决您的反斜杠问题,因为使用节点js启动服务器非常容易

var express = require('express');
var app = express();

app.use(express.static(__dirname + '/www'));

app.listen('3000');
console.log('working on 3000');
创建一个
server.js
文件

const http = require('http')
const fs = require('fs');

http.createServer(function (req, res) {
  fs.readFile('index.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    res.end();
  });
}).listen(3000);
运行
node server.js


这甚至可以通过设置节点web服务器的步骤解决反斜杠问题

  • 从本地计算机创建路由文件夹
  • 从项目根路径转到命令提示符
  • 使用命令npm Install express安装express
  • 创建server.js文件
  • 创建文件夹wwww,并在其中创建Index.html
  • server.js

    var express = require('express');
    var app = express();
    
    app.use(express.static(__dirname + '/www'));
    
    app.listen('3000');
    console.log('working on 3000');
    
    Index.html

    <!doctype html
    <html> 
    <head> 
    <title> my local server </title> 
    </head>
    <body>
    <h1> server working </h1>
    <p> just put your html,css, js files here and it work on your own local nodejs server </p>
    </body>
    </html>
    

    设置节点web服务器的步骤

  • 从本地计算机创建路由文件夹
  • 从项目根路径转到命令提示符
  • 使用命令npm Install express安装express
  • 创建server.js文件
  • 创建文件夹wwww,并在其中创建Index.html
  • server.js

    var express = require('express');
    var app = express();
    
    app.use(express.static(__dirname + '/www'));
    
    app.listen('3000');
    console.log('working on 3000');
    
    Index.html

    <!doctype html
    <html> 
    <head> 
    <title> my local server </title> 
    </head>
    <body>
    <h1> server working </h1>
    <p> just put your html,css, js files here and it work on your own local nodejs server </p>
    </body>
    </html>
    

    谢谢您的回复。在index.html之前我需要一个额外的反斜杠。当前CLI在查找folderNameindex.html时引发错误。我在双引号中尝试了各种向前和向后斜杠,以及os.tmpdir()。有什么建议吗?谢谢你的回复。在index.html之前我需要一个额外的反斜杠。当前CLI在查找folderNameindex.html时引发错误。我在双引号中尝试了各种向前和向后斜杠,以及os.tmpdir()。有什么建议吗?谢谢你。目前,由于连接问题,下载一直超时。我不认为我支持代理,因为我以前成功使用过npm安装。如果你知道这件事,我将不胜感激。谢谢你。目前,由于连接问题,下载一直超时。我不认为我支持代理,因为我以前成功使用过npm安装。如果你知道这个问题,我将非常乐意帮助你。:-)很高兴能帮助您。:-)