How can I run sails console without a webserver (using any ports)? - node.js

I have a Sails server running, and I want to execute some commands from inside of lifted Sails.
The problem is, then I run sails console - it bootstraps another instance of Sails, and trying to load another webserver next to existing, by default using the same ports.
By some environment limits, I can use only one port at the time. So I cannot load another webserver on the same machine.
Is there a way how to run sails console without using any ports?
Thank you.

There is an option. Use sails console --dontLift

Running console without port is I belive not possible at all.
Add inside package json npm script to run console on another port (or if you have sails installed globally just run sails console --port xxxx).
Part of my package json:
"scripts": {
"start": "node app.js",
"console": "sails console --port 1338",
"test": "mocha",
"docs": "rimraf public/docs && apidoc -i config/routes -o public/docs"
},
As you can see... npm run console will run sails console on port 1338 while deafult port of my app is 1337...

Related

Node.js applies the changes only after restart

I am very new to server side scripting. And I am using NodeJS. My Problem is that after adding some new features to the app, i.e. after changing the code, these changes will be applied only after restarting the server. Till then NodeJS behaves so as though I hadn't changed anything. So for instance if I add console.log("works") and don't restart the server, then it hasn't any effect.
I am using Nuxt.js, which is actually the Vue.js framework but with additional and very usefull features mainly for server side rendering. I didn't integrate the express.js at the beginning of the project, beacause it wasn't planned to write any server side code. So I am normally exporting express and using it, which is pretty fine for me, since I need just a couple lines of code to use the NodeJS file system.
So, as it is pretty hard to code, if I should restart the server once I changed anything, I want to ask you if there is any solution to this problem.
Use nodemon
step 1 : npm install -g nodemon <- this will install nodemon globaly in your system
step 2 : change your start script within package.json
"scripts": {
"start": "nodemon fileName" <- like this //filename is you root file which starts the app like app.js
}
step 3 : npm start
This is already build in into nuxt. You just need to run it in dev mode, not in production.
E.g. for dev with change monitoring
nuxt
For production without monitoring
nuxt start
So in this particular case the following changes to the "scripts" in package.json have solved my problem.
"scripts": {
"dev": "nodemon --watch api --exec \"nuxt\"",
"start": "nodemon nuxt",
}
The following link could also be usefull to you.
Install nodemmon in your application to allow live update npm -g install nodemon
and add the following codes inside your packages json file :
"main": "app.js",
"scripts": {
"start": "node app"
},
on your command line, just type : start

How can I pass configuration options to yarn/npm

I have a react app and server that provides REST services to the react app. In development the react app runs on port 3000 and the server on port 3001.
To support this the package.json file has proxy statement "proxy": "http://localhost:3001"
However in production this isn't wanted so I like a means of controlling this from "yarn start" so that I need only one package.json that uses the proxy in development but not in production. Thanks in advance
so that I need only one package.json
you should only have one package.json...
You probably have something that looks like
"scripts": {
"start": "webpack-dev-server --host 0.0.0.0 ..."
...
}
in your package.json - when you run yarn start, ^ that is what is actually being run. That's not something you would run in production though. If you want something else to run in production, add another item to scripts that runs your express (?) app directly (something like node /path/to/index.js is probably close).

Configure Electron/Node To Run In Debug And Attach a Debugger

I'm a Java developer but I have to try and debug a Node based application which runs inside Electron. I use IntelliJ IDEA for Java development/debugging and have WebStorm for which I want to debug the JS application.
As a Java developer I am used to starting the JVM/Tomcat/OSGi container in debug mode to which I can attach IntelliJ as my debugger. This allows me to dynamically add breakpoints without modifying code in IntelliJ. I want to be able to do the same with the Electron application but I haven't been able to work out how to do this.
I have tried starting Election with the --inspect option as detailed here but cannot attach WebStorm to it. I've also tried setting up a run time configuration in WebStorm itself which works as far as starting Electron but terminates with the error 'connection refused'.
I've also used this and this to try and attach a debugger but to no avail. I also have to work on Windows so I'm facing having to deal with inadequate tools to determine if Electron is listening on a port.
Update
I can't put complete code here but I would start the application with:
npm run dev
And this is the relevant part package.json in the root folder of the application but WITHOUT any debugging options specified:
"dev": "concurrently --raw --kill-others \"npm run dev-server\" \"npm run start\"",
"dev-server": "webpack-dev-server --hot --inline",
"start": "cross-env WEBPACK_ENV=dev electron .",
The application starts up i.e. the Election window appears but terminates before it is completely rendered. I can't say for sure how far into the start-up it gets before failing.
Update 2
I've modified the package.json file dev and start lines to:
"dev": "concurrently --raw --kill-others \"npm run %NODE_DEBUG_OPTION% dev-server\" \"npm run %NODE_DEBUG_OPTION% start\"",
"start": "cross-env WEBPACK_ENV=dev electron --inspect=5858 --remote-debugging-port=9223 .",
But still get Connection refused.
To debug Electron main process, you need using Node.js run configuration; for render process, the Attach to Node.js/Chrome configuration is required.
Please see https://blog.jetbrains.com/webstorm/2016/05/getting-started-with-electron-in-webstorm/ for more info
I found that WebStorm can debug the main process properly if electron is started with the flag:
electron . --serve --inspect-brk=5893
Note the 'brk', it stops the process until a debugger is attached.
Then I just created a run configuration (Attach to Node.js/Chrome) and specified the 5893 port, telling to reconnect automatically.

Node Express App debugging with calls from separate client app using WebStorm

I've looked around and had a lot of trouble figuring this out. I'm hoping someone might be able to point me to a post or have information on how to do this.
My problem is that I have 2 projects I've made using WebStorm:
I have 1 application that is my server-side code running on port 3000. It's a simple Node Express app.
The second application is an Angular 4 / Ionic 3 application running the client side on port 8100.
I want to run my server application in debug mode, so that it hits the breakpoints for all the data being sent from the client side app.
For example: Angular / Ionic app sends a get request for all clients for a given customer. The customer is sent via url parameter. I want the server code to pause when it receives this request and so I can see this URL parameter. Fairly simple.
The server is also using grunt to build the project, and nodemon to watch it. I'm using some npm scripts to make life easy. Here are the scripts:
"scripts": {
"dev": "SET NODE_ENV=development && nodemon ./bin/www",
"grunt": "grunt",
"start": "node ./bin/www"
},
Nothing fancy.
I have WebStorm configured to run my scripts from hitting play. So the play button will first run the following sequence:
npm run grunt
npm run dev
Again ... nothing fancy.
Now how do I get this thing to setup a debugger so I can listen in WebStorm? I have a both projects open in separate windows, and I am initiating the calls to the server from the client. How do I make the break points grab hold and show me the data coming into the server?
I feel like this is incredibly easy and I'm missing something really stupid. Any help would be much appreciated.
You need starting you server in debugger to get breakpoints in your server code hit. If you prefer to start your app via npm script, you have to add $NODE_DEBUG_OPTION (or %NODE_DEBUG_OPTION% on Windows) to make sure that Node.js is started with appropriate debug options (--debug-brk, --inspect-brk, etc)
So:
in package.json, modify your dev script as follows:
"dev": "SET NODE_ENV=development && nodemon %NODE_DEBUG_OPTION% ./bin/www"
right-click your package.json, choose Show npm scripts
right-click dev script in npm tool window that opens, choose edit 'dev' settings to create a run configuration.
open your source files in editor, add breakpoints
press Debug to start debugging
run your client app, initiate the calls to the server

How to stop nodemon from changing port on each restart of express app?

I am new to nodejs and this is first time I am using nodemon. I am using nodejs on windows. I have got following in my package.json file
"scripts": {
"start": "nodemon ./bin/www"
}
And I use npm start from command line to start my express app. The process start with a default port which is annoying. But what is even more annoying is that every time I change a file nodemon restarts the application, sometimes on an entirely different random port number. I tried changing the script section in package.json file to the below but that did not make any difference
"scripts": {
"start": "nodemon ./bin/www 3000"
},
From the comments it seems you're specifying the port through an env variable, let's call it EXPRESS_PORT. The node process doesn't inherit it when you start it with npm because npm start creates a new shell with its own environment. So you end up passing port undefined to express. That makes it bind to a random free port. To fix this you can set the variable in the start command:
"scripts": {
"start": "EXPRESS_PORT=3000 nodemon ./bin/www"
}
Or you can export it from your shell with export EXPRESS_PORT=3000 and then run npm start. If you do this you need to make sure to always export before starting the server, so you might want to place the export in ~/.profile or ~/.bashrc.

Resources