English 中文(简体)
Node.js Code to open a page in browser with localhost URL
原标题:node.js code to open a page in browser with localhost URL

我已经用 no子书写了一个简单的服务器。 目前,服务器通过向浏览器撰写“hello世界”。

服务器档案类似:

var http = require("http");
http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}).listen(8080);

我在浏览器中使用这个URL来启动“hello World”反应:

http://localhost:8080/

在我通过这样一部《世界语言》时,我想能打开一个基本的html页:

http://localhost:8080/test.html

我通过许多辅导员和一些中途员额,但这项任务没有太多。 是否有任何人知道如何通过简单修改服务器来做到这一点。 j 档案?

问题回答
yarn add express open
import open from  open ;
import express from  express ;

const port = 3000;
const app = express();

app.get( / , async (req, res) => {
  res.send( Hello, Coder! );
});

app.listen(port, () => {
  console.log(`listen port: ${port}`);
  open(`http://localhost:${port}`)
});

第二个变量:

const http = require( http );
const open = require( open );

const PORT = 3000;

http.createServer(function (req, res) {
  res.writeHead(200, { Content-Type :  text/html });
  res.write( <h1>Hello World!</h1> );
  res.end();
}).listen(PORT, function () {
  console.log(`Server running on port ${PORT}`);
  
  // Open the server URL in the default browser
  open(`http://localhost:${PORT}`);
});

如果希望在“

最好是使用头端 j子框架,如安热、反应或维埃,前往不同网页。 虽然你想在诺德这样做,但你可以这样做:

var express = require( express );
var app = express();
app.get( / , function(req, res) {
  res.sendFile( views/index.html , { root: __dirname })
});
app.get( /test , function(req, res) {
  res.sendFile( views/test.html , { root: __dirname })
});
app.listen(8080);

这是固定网页的ok解决办法。 快递非常有助于撰写《教育、科学和技术年鉴》。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签