简体   繁体   English

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

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

I have the following server configuration code using Express.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}`);
});

So, I wanna assign a domain name,所以,我想分配一个域名,

For example: I wanna use www.example.com:5000 instead of localhost:5000.例如:我想使用 www.example.com:5000 而不是 localhost:5000。 How should I configure domain name for my local server ?我应该如何为我的本地服务器配置域名? Any links I could refer to is much appreciated.我可以参考的任何链接都非常感谢。 Thank you.谢谢你。

At first, the localhost point to loopback IP 127.0.0.1 .首先, localhost指向环回 IP 127.0.0.1 This is defined on hosts file located at /etc/hosts , for Linux systems, and at c:\\windows\\system32\\drivers\\etc\\hosts , for Windows.这是在位于/etc/hosts主机文件上定义的,对于 Linux 系统,在c:\\windows\\system32\\drivers\\etc\\hosts ,对于 Windows。

On hosts file, you can see, the entry:在主机文件上,您可以看到条目:

127.0.0.1 localhost 127.0.0.1 本地主机

The hosts file is used preferentially to other name resolution methods, even before looking in DNS.主机文件优先于其他名称解析方法,甚至在查找 DNS 之前。

Thereby, if you intend run your app locally available at www.example.com , just add the bellow entry on hosts file:因此,如果您打算在www.example.com本地运行您的应用程序,只需在主机文件中添加以下条目:

127.0.0.1 www.example.com 127.0.0.1 www.example.com

On other hand, if you intend run you app over internet avaiable at www.example.com you should register this host with some DNS service.另一方面,如果您打算在www.example.com上的互联网上运行您的应用程序,您应该使用某些 DNS 服务注册该主机。

Updated:更新:

search about 'host file' or 'hosts file'搜索“主机文件”或“主机文件”

and I added a comment with some useful links我添加了一个带有一些有用链接的评论

good luck祝你好运

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

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