Is there a simple html-webserver for mac? - javascript

Sometimes when I do front-end development, I use MAMP as a web-server so that I don't have to deal with CORS-errors. Since I upgraded to mavericks MAMP won't work any more and it is pretty heavy. Is there something simpler or more lightweight built in or easily installed?

http-server
Install:
npm install http-server -g
Start:
http-server -p port -a address

depending on how simple you like it:
Python 2:
python -m SimpleHTTPServer 8000
https://docs.python.org/2/library/simplehttpserver.html
Python 3:
python3 -m http.server
https://docs.python.org/3.3/library/http.server.html

XAMPP is a well-known local webserver. I'm told it works quite similar to MAMP

Navigate to the folder you want to open and write:
python -m SimpleHTTPServer 8000
8000 is the port, you may change it.
The terminal will respond with something like Serving HTTP on 0.0.0.0 port 8000 ...
if you navigate to the adress 0.0.0.0:8000 you will see your webpage.

php -S localhost:3000
Or the port you want.

Related

Http-server running in background with nohup doesn't remain live

I have an ubuntu server. On which I have transferred some files which have some js and html code.
I used http-server from node.js to start a web server so that I can display the html page on the server.
I used nohup so that it can remain running even if I disconnect or close my system.
Here is the command I used:
nohup http-server -p 8000 -a 10.4.145.182 &
Now this helps me visualize the files on 10.4.145.182:8000 but I am noticing after sometime the server goes down and one can't access the html page on that ip in their browser.
I thought nohup helps run things in background even if one closes their system or logs out of server.
How do I make this web server running always then and accessible to url to everyone
Thanks
EDIT:
As per the suggestion below of using pm2, I installed latest version of node and then started service with pm2.
It says service started for http-server but when I go the to ip with port 8000 on browser it doesn't open up.
Here is the command I ran in my directory which has the html and d3 files.
$ pm2 start $(which http-server) -p 8000
And here is the output in shell of the pm2
Try this:
http-server & exit
You can also specify a port number:
http-server -p 8082 & exit
Reference here
I suggest you use pm2 . Long story short , check the official link https://www.npmjs.com/package/pm2
Steps :
Install :
npm install pm2 -g
Run your app :
pm2 start app.js
In your case use :
pm2 start /usr/local/bin/http-server -- -p 8080
for reboot to work run :
pm2 startup systemd
take the last line , change the user and home path and run the modified line as SUDO !!! you need sudo access for this !!!

keep getting "? Something is already running on port 3000" when I do npm start on react app

I keep on getting "? Something is already running on port 3000" message in my terminal when I start up my react server when there is absolutely nothing running on my port 3000
What I have tried to solve with:
Restart the macOS.
checking my "http://localhost:3000" on chrome browser. (Nothing: This site can’t be reached)
Go to chrome://serviceworker-internals and chrome://appcache-internals, search for localhost:3000 (Nothing found)
I also tried almost every command lines I found on Google regarding this issue
I also created another express.js app, and it was successfully launch on port 3000 while React said there is something running on 3000. React on my computer just keeping thinking there is something running on port 3000.
If you are a windows user you can try using
npx kill-port 3000
in your console. I was having the same problem and it worked for me.
Not sure about MAC.
Open cmd and write this
netstat -a -o -n
You will get list of active connections then find 3000 by hitting
Cntrl + f
Copy the PID of that port and hit this command
taskkill /F /PID PID_of_port
Edit
This guide is for windows.
Kill Node.Js process from Task Manager.
Step 1: Open Task Manager by clicking ctrl+shift+delete
Step 2: Open Prcesses tab
Step 3: Search for Node.JS process and right click on that then click on End Task
Step 4: Now you can start again.
As #khurram khan suggested terminating the process may be the best option for you, This work for me on linux:
$ lsof -i tcp:3000
$ kill -9 PID
the first command should give you the PID number to enter in the second command as PID.
I had this problem on Mac and I solved by running:
npx kill-port 3000
I had stuck with this one for few hours, and the end I had found the solution
There was incorrect mapping to the local host in the hosts file and didn't had any relation to the port taken something like
10.2.224.130 localhost
Just Change it back to
127.0.0.1 localhost
Host file locations
https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/
In case anyone comes across this and the above solutions didn't help, make sure your /etc/hosts has:
127.0.0.1 localhost
For some reason this was wiped from my hosts file and CRA's dependency for checking ports (detect-port-alt) checks localhost:[PORT] to see if it's available. If it errors out, you'll always get the "Something is already running..." error when checking a specific port but not when using a random port (since that's picked by the dependency).
kill node.js from the background processes. that would soleve the probelem. alterntively, you could restart your system
for window use this in package.json
"start": "set PORT=3006 && react-scripts start"
for Linux and MacOS
"start": "PORT=3006 react-scripts start"
This worked for me on windows pc. This one is for those are not seeing the port when you run this command netstat -a -o -n on your command prompt.
Open your command prompt in administrator mode and run this command
net stop winnat
you'll get this response:
The Windows NAT Driver service was stopped successfully.
Them you run this next:
net start winnat
then you will get this response:
The Windows NAT Driver service was started successfully.
once you do that. Start the react server and it would work. Same too if your backend server doesn't run on 3000
It's very simple. You can fix it in 2 easy steps.
Check your environment variables if there is a key/entry with name "PORT".
If found delete that entry or rename it to something else.
It turns out that some other program is using that variable. Usually when you start react-scripts it will look for an environment variable with that title PORT.
Date: Sat 07, October 2020
Windows: Microsoft Windows 10 Pro Build 19041
Node: 12.16.1
NPM: 6.14.8
Something similar was happening to me on my Windows machine. Tried a lot of things suggested here on StackOverflow and other places.
In my case, I was following a video course that was suggesting adding --script-version 1.1.5 (as illustrated below) when creating a new React app.
create-react-app app-name --scripts-version 1.1.5
Here's the package.json scripts versions:
React: 17.0.1
React DOM: 17.0.1
React Scripts: 1.1.5
After running the command npm start and typing y to try to use a different port, the terminal will stay stucked until it was canceled.
Then I created a new React app without --scripts-version 1.1.5 and after trying to running it, it was still running into Something is already running on port 3000 but after typing y, the app will run with no problem on a different port.
Here's the package.json scripts versions (when it was running successfully):
React: 17.0.1
React-Dom: 17.0.1
React-Scripts: 4.0.0
Starting with the reason you do not see anything on localhost:3000, is because there must be a proper webapp or a website/server running on that port, but the port:3000 is currently running some process, just not of a server/site/app.
This out of the way, depending on your type of OS, it is quite easy to check if there is in-fact a process on port:3000 or not. For MacOS, opening a terminal and running sudo lsof -i ':3000' will list the current process on port 3000.
Note that we need to run command as sudo or root
Once you see what process is running, note the PID and run kill -9 {PID}, which should kill the process with PID (i.e distruptive process on port:3000). Now doing a npm start should get your app built, compiled and served on localhost:3000
Mostly what might've happened is that you had a npm start running which you either interrupted, or closed in the foreground. Doing a fg in your terminal will open any background tasks, so if this opens a npm start instance, it was this process stopping your flow. You can then properly end the session making sure all processes on port:3000 are killed.
Happy coding
If you are on linux you can try
pkill -f node
to terminate the processs
The error occurs when a task is left running on the port properly killing it.
this error can occur on both ports. Your react app and your node server.
To solve this you can run the following commands.
netstat -anp tcp | grep 3000
This command in the terminal will give you the list of activities on port 3000
npx kill-port 3000
This command will kill all the running servers on port 3000.
Now if you have the same problem for your Node server. You can follow the same steps.
netstat -anp tcp | grep 5000
Then
npx kill-port 5000
You don't need the first command. it's just to list out the running activities on the port.
just change the port number for any other port number.
This is the output you get after running the commands.
I am using Git bash on windows.
For me, this works every time (macOS): sudo kill -9 $(sudo lsof -t -i:3000)
Simple and Easy solution
close your current terminal and open a new one.
If you are running it in VS Code just create a new terminal and delete the old one.
On React - you can run an already created React single-page application (SPA) by
npm start command.
That may start your locally hosting development server and runs your app at:
http://localhost:3000/ which is equivalent to: 127.0.0.1:3000 address
127.0.0.1 is the default localhost IP number while the default port number set by
create-react-app package is 3000.
When getting: “Something is already running on port 3000" failure error message you may think that the port captured by another process running on your machine but you’ll find that it is captured permanently as if it runs on 0.0.0.0:3000 address
Solution:
In your project libraries created by create-react-app script navigate to:
node_modules/react-scripts/scripts/start.js
While running npm start command - the start.js script is being called and executed
There at start.js file in you editor find the above line:
const HOST = process.env.HOST || '0.0.0.0';
and change it to:
const HOST = process.env.HOST || '127.0.0.1';
save and run your web app again at: http://localhost:3000/ or http://127.0.0.1:3000

How to run html file on localhost?

I have an HTML file and I run it on localhost. But, this file includes a mirror using a webcam. For example, how can I run this HTML file on localhost? Webcam starts in this example when checking to live checkbox.
You can run your file in http-server.
1> Have Node.js installed in your system.
2> In CMD, run the command npm install http-server -g
3> Navigate to the specific path of your file folder in CMD and run the command http-server
4> Go to your browser and type localhost:8080. Your Application should run
You can use python -m http.server. By default the local server will run on port 8000. If you would like to change this, simply add the port number python -m http.server 1234
If you are using python 2 (instead of 3), the equivalent command is python -m SimpleHTTPServer
If you are running Python3, you may want to instead try:
python -m http.server
See this answer.
Install Node js - https://nodejs.org/en/
go to folder where you have html file:
In CMD, run the command to install http server- npm install http-server -g
To load file in the browser run - http-server
If you have specific html file. Run following command in CMD.- http-server fileName
by default port is 8080
Go to your browser and type localhost:8080. Your Application should
run there.
If you want to run on different port: http-server fileName -p 9000
Note : To run your .js file run: node fileName.js
If you have Node.js installed then from the folder you want to share you can simply run:
npx http-server
To add CORS you can run:
npx http-server --cors
On macOS:
Open Terminal (or iTerm) install Homebrew then run brew install live-server and run live-server.
You also can install Python 3 and run python3 -m http.server PORT.
On Windows:
If you have VS Code installed open it and install extension liveserver, then click Go Live in the bottom right corner.
Alternatively you can install WSL2 and follow the macOS steps via apt (sudo apt-get).
On Linux:
Open your favorite terminal emulator and follow the macOS steps via apt (sudo apt-get).
As Nora suggests, you can use the python simple server.
Navigate to the folder from which you want to serve your html page, then execute python -m SimpleHTTPServer.
Now you can use your web-browser and navigate to http://localhost:8000/ where your page is being served.
If your page is named index.html then the server automatically loads that for you. If you want to access any other page, you'll need to browse to http://localhost:8000/{your page name}
You can try installing one of the following localhost softwares:
xampp
wamp
ammps server
laragon
There are many more such softwares but the best among them are the ones mentioned above. they also allow domain names (for example: example.com)
You can install Xampp and run apache serve and place your file to www folder and access your file at localhost/{file name}
or simply at localhost if your file is named index.html
You can also use PHP to server the files in http
make sure you installed PHP, run the below command to verify
php --version
if PHP is not installed run below command to install it
sudo apt install php7.4-cli
Once after the installation go to the file path and execute the below command in the terminal
php -S localhost:8000
just npx serve it's more compatible with esmodule

Getting This webpage is not available response when starting up node.js server

I'm a beginner at node.js, and I don't know what I'm doing wrong when trying to access my server. Here are the things I wrote in cygwin to start up the server:
1) npm install http-server -g
2) http-server
Now, I access the server at http://0.0.0.0:8080, but it isn't working; it gives a "This webpage is not available" error. Can anyone tell me what I'm doing wrong? Please, don't hesitate to ask for more information if necessary.
Start with http-server -a 127.0.0.1 and try accessing http://127.0.0.1:8080 or http://localhost:8080
0.0.0.0 is not a good default and doesn't work on Windows.
In the console type
http-server -a 127.0.0.1
then in the browser try:
http://127.0.0.1:8080
or
http://localhost:8080
be aware of have a index.html in the curren dir

Node.js http-server and a rewrite rule

I'm using the node.js module http-server for developing an AngularJS application. Now I want to add a rewrite rule for this server: Every request with the pattern:
http://localhost:8000/api/*
should be redirected to
http://apiserver/api/*
All other request should be handled by http-server. But I'm not very sure what the best solution is here. I don't want to have an extra Node.js script. I just want to start one server. Is that possible? Is http-proxy a solution?
Yes you are on the right track. Personally, I would use Nginx over node-http-proxy. All you have to do is install nginx (assuming under Ubuntu)
sudo apt-get install nginx
now open your nginx settings file
sudo rvim /etc/nginx/sites-enabled/!? (whatever that is under sites-enabled)
and configure your paths. You will setup two upstream and one redirect route to apiserver/api/*, and proxy the rest to localhost:8000. For a more detailed guide, check out http://www.cyberciti.biz/tips/using-nginx-as-reverse-proxy.html

Categories