How to create NodeJS server using http-server? - javascript

I am new to Nodejs and would like to know how to use http server using the modejs http-server module.
I did the following steps:
1. Installed Nodejs.
2. Installed http-server module using the command : npm install http-server -g
3. Created a folder with a HTML doc
Once I start the server using the command http-server, the URL provides the following output:
192.168.1.5 sent an invalid response
CMD commmand :
C:\Users\user\folderLocation>http-server
HTML output :
192.168.1.5 sent an invalid response
Pls. do help with with this issue and provide feedback with what i'm doing wrong.
Thanks in advance!!

http-server -p8080 . //serves current directory
http-server -p8080 ./public //serves directory named public
https://www.npmjs.com/package/http-server

Apparently there is a bug in a dependency of the recent version of http-server. One solution is to install http-server#0.9.0

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 !!!

deploying js with python and spacy lib

I have never setup things this way, I got it running locally but have no clue how I should have do it when I deploy it into an ec2 server
I have a node app running already in ec2 server but recently I have added spaCy into one of my api.
The basic logic is calling the api which runs node and part of the js script uses spawn to process the proc.py file and do some calculation with outputs.
I have tried the following below to setup on my server but of course it failed when calling the api
my original node app is in /var/www/myApp
I used sudo apt install virtualenv then inside my /var/www/myApp I ran the command virtualenv venv which creates the virtual environment...
I did a source venv/bin/activate to get into the virtual environment
Then I started running the commands to install spacy mentioned in the spacy documentation https://github.com/explosion/spaCy
used this command to install pip install -U spacy and python -m spacy download en
Then I ran python -m spacy validate to see if spacy is installed which I got
Installed models (spaCy v2.0.11)
/var/www/myApp/venv/local/lib/python2.7/site-packages/spacy
TYPE NAME MODEL VERSION
package en-core-web-sm en_core_web_sm 2.0.0 ✔
link en en_core_web_sm 2.0.0 ✔
which I believe this means the installation is a success.
But after this, I am not sure what I should do to continue.
I tried running my api but getting this as an error message "err": "Traceback (most recent call last):\n"
I would believe the spacy module is not being called that's why I am getting this error but I am not sure what I should do to get this working.
Would really appreciate for any help, thanks in advance for any advices.
EDIT:
my proc.py code is below
if __name__ == '__main__':
import sys
import json
import spacy
nlp = spacy.load('en')
text = sys.argv[1]
doc = nlp(text)
all_noun_tokens = [ token.lemma_ for token in doc
if (token.pos_ == 'PROPN'
or token.pos_ == 'NOUN'
or token.tag_ == 'NN'
or token.tag_ == 'NNP') ]
print(json.dumps(all_noun_tokens))
P.S. I am wondering if there's any config that I need to do in order for my app to find where the virtuanenv is so it can detect the spacy module?

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

MeteorJS : How do I check the data fetching from mongoDB on console client?

We have deploy the MeteorJS code on Ubuntu server. We haven't created any client to access this MeteorJS service. First we wanted to verify the service are running properly. Can any one suggest the steps for check the deployment is correct.
We install Meteor on Ubuntu : curl https://install.meteor.com/ | sh
MonogoDB - Already install for NodeJS application - Its Working!
Copy the Code Meteor Code to server using WinSCP
Set the MongoDB path for Code : export MONGO_URL=mongodb://ip:27017/userDB
Run Command to start the app: sudo nohup meteor --port 3001 --production &
Thanks.

Installing node.js for angular - connect.static and karma

I'm trying to install a simple node webserver following the example in Pro AngularJS from apress.
I've installed node.js and the connect and karma modules.
I do get a warning when I installed karma via: "npm install -g karma" that says "optional dep failed, continuing" but then seems to install correctly.
I created a server.js based on the example:
var connect = require('connect');
connect.createServer(
connect.static("../angularjs")
).listen(5000);
when I run it I get TypeError:Undefined is not a function pointing to connect.static.
Apparently in the latest build of connect the static middleware has been moved to it's own package.
nodejs connect cannot find static

Categories