Why won't my express js app execute? - javascript

I'm having trouble executing my express js app on my localhost. When I type:
christian#ubuntu:~/node_modules/nave/create$ node app.js
I get this as the output:
axconfig: port 1 not active
axconfig: port 2 not active
I have no idea why this is happening, last time I executed it, it worked perfectly, then my laptop crashed once, and now it won't execute. Any help for this is greatly appreciated.

From this post, it sounds like you aren't actually running NodeJS, but node the radio application. You have likely used apt-get to install the wrong program.
Perhaps you ran apt-get install node and installed the wrong thing. If you are not using the radio application, you should apt-get install node to remove it since it is an entirely different program and not related to nodejs at all.
Currently when you type node into your prompt, it is finding /usr/sbin/node, which is the radio application. You should be running /usr/bin/node if you want to use NodeJS. In order for node to be resolved, you need to remove the radio app, or have /usr/bin in your $PATH before /usr/sbin

Related

Node js server auto restart [duplicate]

I am running a node.js application in Windows and I want to make it automatically restart if there is an unhandled exception in the code which causes the application to stop.
I have done some research and I found that a combination "Forever" and "Nodemon" can achieve this goal.
I installed both packages globally on my Windows 10 Device.
npm install forever -g
npm install -g nodemon
I tried using the following command to launch my app:
forever start nodemon --exitcrash app.js
However, I get the following error: "nodemon does not exist"
If try just running "nodemon" the application starts which indicates the Nodemon package is installed however, this will not allow the app to restart after a crash.
Am I doing something wrong? Most advice I find online is only relevant to Linux systems.
If you are already using forever, then you can get rid of nodemon. Instead you can use a combination of forever and cluster module. Simply fork the worker in case of exceptions, and it makes your app more scalable too!
If still nodemon is preferable, maybe try installing it globally using the -g flag
Forever and nodemon achieve 2 completely different objectives
nodemon is used to run your application in development mode, where you are frequently changing code, and need to restart the server .It will not restart your application in case of a crash. more about that later
Forever, on the other hand, is for making your application run as a daemon in production. And auto restart if you have uncaught exceptions.
Historically people have used Forever stand alone, or with upstart scripts, running as a linux service one of the most famous being upstart
Current norm is to use PM2

Nodemon not working on new project with express

I have a new project folder configured and running with express.js and the server is running fine on port 3000.
I have installed nodemon globally using the sudo command so I expect I don't need to add this as a dependency or locally within the project.
When installing nodemon there are no errors, however when i fire the command nodemon server.js the command line essentially doesn't do anything and stops accepting commands.
I'm wondering if I'm missing something and hoping someone can point me in the right direction.
Thanks in advance for your time.
Use npm install -g nodemon this command to install nodemon globally and try to run nodemon [your node app]. For better understanding go through link.
Evening all, thanks for your contributions earlier, essentially things were mixed up with the course since the updates which was causing my issue.
Previously it looks like you would install express and run server.js through your designated port and then boot up nodemon. This no longer works and was causing the issue I was having.
Now you simply just boot up nodemon via the command nodemon server.js and it handles the rest. This wasn't really outlined in the documentation.
I was expecting to boot up localhost and then use nodemon to watch for changes. Looks like the new method is much more efficient
Thanks for the help

Writing a correct Dockerfile

I created an app using Javascript (with D3.js), JQuery, CSS, but no Node.js. It's your typical 'index.html' browser-run interface. I've been going through Docker tutorials and trying to figure out how to set my app up to a server, but I've had no luck accomplishing it and have only been able to find tutorials using apps with Node. I cannot, for the life of me, figure out what I'm doing wrong, but I'm wondering if the problem (or one of them) lies in my Dockerfile. Also, do I need to have used Node.js for all this to work? My app consists of the following:
A directory called Arena-Task. Within this directory, I have my index.html, my main javascript file called arena.js, and my CSS files. My other necessary files (like images, etc.) are located within two other folders in the same directory called data and scripts.
So now, how would I write my Dockerfile so that I can build it using Docker and publish it to a server? I've attempted following Docker's example Dockerfile:
FROM node:current-slim
WORKDIR /usr/src/app
COPY package.json .
RUN npm install
EXPOSE 8080
CMD [ "npm", "start" ]
COPY . .
But to be honest I'm not quite sure how to make the changes to accommodate my program. I can't figure out if a package.json is required because if it is, then don't I need to be using Node? I don't use any node modules or project dependencies like that in my app. Do I need to be? Is my problem more than just an incorrect Dockerfile?
Sorry that this question is all over the place, but I'm really new to the realm of the server-side. I appreciate any help! Let me know if I can provide any clarification.
lets clarify few things:
node and npm is when you need them, like you are using some npm packages.
package.json - is in use by npm - it stores installed package list in it.
For you case i don't see need of node. so you can create simple image and then you going to need simple web server - something which can serve you html/css/js files on web requests over http. the simplest i know would be nginx.
Also in Dockerfile you need to copy all you resources into image you are building.
that is what COPY package.json . was doing. but in you case you have to copy whole app folder into some app folder in docker image. (assuming app is a folder which holds all you files)
so we are going to have steps
Dockerfile should look something like this:
FROM ubuntu
RUN apt-get install -y nginx
COPY app app
COPY startup.sh /startup.sh
COPY ./nginx-default /etc/nginx/sites-available/default
no need in default commands because you going to start something else during docker run.
nginx-default is a configuration for nginx to work as webserver:
it should look something like this:
server {
listen 8080;
server_name localhost;
root /app
}
nginx is very flexible - if you need something from it google it.
docker image should do something all the time, otherwise image going to stop (some blocking process).
the easiest way i know is to create startup.sh file which going to start nginx as first step and then going to do infinity loop:
exit_script() {
trap - SIGINT SIGTERM # clear the trap
sudo service nginx stop
exit 1
}
sudo service nginx start
while sleep 20; do
CURRENT_TIME=$(date +"%T")
echo "app is running: $CURRENT_TIME"
done
exit_script - is a trap which helps to stop docker image in fast way, but not like terminate. but you can omit that for testing purposes.
finally, build image (docker build -t {your-image-name} .) and to start image use something like this:
docker run -p 8080:8080 {your-image-name} bash /startup.sh
that should work :), though most probably you going to face few errors because i was writing it from the head. (like you may need something else for nginx, or sudo is not installed by default in ubuntu lates image).

How do I run a copied project in Node.js

https://github.com/gleitz/mahjong
I want to run this app on my windows,
the directions say:
-Install dependencies with npm update
-Start the application with node app.js
it's sound easy so I try myself.
1.Fist of all , I install node.js on it's official website (https://nodejs.org/en/)
I download the 8.9.3 version.
after installing node.js
2.open the command line and go to the project path.
3.then I input the command npm update.
it works,and the node_modules folder is created.But there are some warn message
4.finally input the command node app.js... it's not work with many error message
following is wrong message
I wonder know how should I do.Is any thing I didn't installed?
Please help me.I really want to research this mahjong project.
Before running npm update in the directory where you have your project, you should run npm install first to install all the required dependencies needed for the project to run.
So I advice to delete the node_modules directory that was created after you ran npm update, after which you can then run npm install. This should solve your issue.
Update: If you just want to try it online and not run it locally they've hosted a version on the web: http://gleitzman.com/apps/mahjong.
It's not your fault.
What you did was correct, but the project documentation needs to be updated. It's not a turn-key solution and you'll need to figure a few things out to get it working.
The error message is says it wants a mongo db instance, but you don't have one running. Try the mongodb home page or google for instructions. If you have docker it is pretty easy: docker run -it -p 27017:27017 mongo.
Even after spinning up mongodb I wasn't able to get the app to work locally. You could try contacting the repo maintainer for assistance. They may be happy to help given you've shown interest in their project.
Good Luck!

Node compile on file change

I'm doing small programming challenges locally.
In one tab I draft a solution, let's call it challenge.js and in another tab I run the command node challenge.js whenever I make a change.
Is there a way for node to compile automatically whenever a change has been made to challenge.js?
The first comment answered the question.
I installed nodemon:
$ npm install nodemon -g
$ nodemon app.js
and it works perfectly.
This is for development environment?
If yes, you can use a build tool like webpack, gulp, etc.
Webpack provides a tool webpack-dev-server, that recompile your bundle at every change.
I hope to have helped.
Use nodemon. https://www.npmjs.com/package/nodemon I work in server development and it refershes the server every time I save a file. If its a case you want something live to restart every time it crashes or if theres any change use pm2
https://www.npmjs.com/package/pm2

Categories

Resources