exposed or just local ? in general @lichtjaeger is right although as a devops guy I don't like "language specific"-examples
if you have a free port 80 and/or 443, which are the default ports of the browser for http/https connections and you are connected to a LAN, you can expose your machine to serve as a web-server. (or any other server if you so choose)
if you're building an intranet you can configure your router so it uses dnsmasq or something similar to give a local TLD (top level domain)
myintranet.lan for example
if you configure it right your machine name (example: my-laptop) can be used as a specific identifier
if you add a nginx/apache/node/c++/whatevermakesyouhappy server on port 80 to it you usually have to set a name for the host which has to match with the hostname in the GET request.
for example :
myapp.my-laptop.intranet.lan -> <appname>.<machine-identifier>.<domain>.<top-level-domain>
which can be seen generalized as <subsubdomain>.<subdomain>.<domain>.<top-level-domain>
if you set the foundation right now everyone inside the network can access everyones elses hosts if needed.
If you wanna host things via the internet you would need a static IP and a DNS entry but it's basically the same. you could just remove the <my-machine> part if you want.
oh and VPNs are ofc a possibility if you wanna be more secure :)
Yes, you can.
To access the website from your laptop type "http://localhost:<port number>" as the address in your browser. You don't need ":<port number>" if you use the default port (80).
You can access the page from another computer on the same network by using "<name of your laptop>:<port number>".
A simple web server in Node looks like:
const http = require('http')
http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' })
res.end('Hello World!')
}).listen(80)
Caleb whiting
Yes! I'll tell you how I usually do it.
Just to be clear before we start, it's a bad idea to make a full-fledged web server on your laptop. There are very cheap services where you can run real web servers. Most web developers, however, need to set up temporary web servers for different reasons on a regular basis. This guide will show you how.
Creating the web server
You may already have a server
If you're a web developer, I expect that you already start a web server as part of your day-to-day work. If you go to
localhost:3000in your browser, then that's already a web server and you can skip to the next step.Simple File Server
I create a file server on my mac all the time. Like this:
$ python -m SimpleHTTPServer 8000It will start serving files on port 8000. You can see the result by going to localhost:8000. This is my favorite way to transfer files between computers.
BrowserSync server
This one is pretty cool. If you use BrowserSync, it will give you a server for free and also auto-update all the connected devices every time you change the code.
$ npm install -g browser-sync # do this once to install browser-sync $ browser-sync start --server --files "*" --port 8000Advanced Method
If I need a more advanced web server, I just drop in a stock express.js server. Not everyone uses node.js, but I see that you do so I'll hope you won't mind. I won't go into the details here, but by tweaking this file you can serve from multiple directories, integrate socket.io, require passwords for certain folders, and so on.
const app = require('express') const morgan = require('morgan') const app = express() app.use(morgan('dev')) app.use(express.static(__dirname)) // change this line if you want to serve on something besides port 8000 app.listen(8000)Then in terminal:
$ npm init -y # you've probably already done this step $ npm install --save express morgan # do this once to install express and morgan $ node server.js # do this every time you want to launch the serverNow you can open your server in your browser at
localhost:8000Accessing the web server from another computer
Now say you want someone else to see your server. You can access it on your laptop at
localhost:8000, but how do you get to it from another computer? The first step is to find your ip address. If you're using browser-sync it will tell you when you launch, but otherwise you'll need to look it up. If you're on a windows, you can find it like this:You'll see a whole bunch of information printed out and buried in there will be the ip address. If you're on a mac, you can find this in System Preferences > Network
The number highlighted in red is your local IP Address.
Let's say that your local ip turns out to be
192.168.1.47. That means you can access your server from another computer by going to192.168.1.47:8000in the browser. Cool!Losing the
:8000All my examples so far require you to add :8000 to the end of the URL. You don't have to do that though. Port 80 is default, so if you serve from that you can just access your site from
localhostor192.168.1.47instead oflocalhost:8000or192.168.1.47:8000.But if you try to start a server that way, it will give you a permission denied error. That's because you need administrator privileges to serve from any port number that's lower than 1024. To solve this, just add sudo before the command to start your server. This will prompt you for your administrator password before starting.
For the Python server:
$ sudo python -m SimpleHTTPServer 80For browser-sync
$ sudo browser-sync start --server --files "*" --port 80For an express.js server
Accessing the web server from another part of the internet
The one caveat to the previous example is that you can't access the server from another part of the internet. The computer accessing the server has to be on the same network as your computer. That's good because it keeps hackers from messing with you, but sometimes you really do want it to be public. There are two ways:
The Standard Official Way
This is the method that you should use if you really want to serve a website with a standard domain, like mysite.com. I won't go into details, but this should get you started. In brief the steps are:
The Quick and Dirty way
If you don't want to set up a permanent server and just want to, say, let someone else temporarily see what you're working on, you can use Ngrok.
Now comes the terminal work
$ cd path/to/ngrok # wherever you extracted the zip $ ./ngrok authtoken b5ES7AyAytsbWNX2AY # you got this when you signed up $ ./ngrok http 8000 # replace 8000 with whatever port you're serving on.You should see a success screen that gives you a URL to access your site from anywhere in the world:
In my case it's
d0affdd3.ngrok.io(Notice that ngrok always serves on port 80, even if my app serves on port 8000.)Notes
I hope that helped. Probably you know some of that already, but I couldn't tell your knowledge level from the question. Hopefully this answer can be useful to someone else as well.