简体   繁体   中英

Configure the domain name for local server using Node.js

I have the following server configuration code using 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. 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 . This is defined on hosts file located at /etc/hosts , for Linux systems, and at c:\\windows\\system32\\drivers\\etc\\hosts , for Windows.

On hosts file, you can see, the entry:

127.0.0.1 localhost

The hosts file is used preferentially to other name resolution methods, even before looking in DNS.

Thereby, if you intend run your app locally available at www.example.com , just add the bellow entry on hosts file:

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.

Updated:

search about 'host file' or 'hosts file'

and I added a comment with some useful links

good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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