繁体   English   中英

使用 Node.js 为本地服务器配置域名

[英]Configure the domain name for local server using Node.js

我有以下使用 Express.js 的服务器配置代码:

const port = process.env.PORT || 5000;

const express = require('express');
const bodyparser = require('body-parser');
const app = express();

const path = require('path');
const api = require('./apiUrl')({});


app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: false }));

app.use(express.static(__dirname))
app.use('/api', api);

app.listen(port, ()=>{
    console.log(`Server Listening to the port ${port}`);
});

所以,我想分配一个域名,

例如:我想使用 www.example.com:5000 而不是 localhost:5000。 我应该如何为我的本地服务器配置域名? 我可以参考的任何链接都非常感谢。 谢谢你。

首先, localhost指向环回 IP 127.0.0.1 这是在位于/etc/hosts主机文件上定义的,对于 Linux 系统,在c:\\windows\\system32\\drivers\\etc\\hosts ,对于 Windows。

在主机文件上,您可以看到条目:

127.0.0.1 本地主机

主机文件优先于其他名称解析方法,甚至在查找 DNS 之前。

因此,如果您打算在www.example.com本地运行您的应用程序,只需在主机文件中添加以下条目:

127.0.0.1 www.example.com

另一方面,如果您打算在www.example.com上的互联网上运行您的应用程序,您应该使用某些 DNS 服务注册该主机。

更新:

搜索“主机文件”或“主机文件”

我添加了一个带有一些有用链接的评论

祝你好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM