Start server.js automatically

I would like the nodemon server.js to automatically execute as soon as I execute the ionic emulate ios or ionic emulate android

How could I configure it to work that way?

This is because I have a service in mongo, and it needs to be execute the connection with the mongo that is implemented in server.js

Should be as simple as updating the npm script.

can you give me a example. ?

I mean, NPM is a well documented tool with many blog posts and examples, and great official documentation that walks through managing scripts…

But it’s basically:

"preionic:serve": "echo \"RUNNING BEFORE SERVE\" && exit 0"

I’m sorry.

I don’t understand.

look my example:

image

I want startup, but I do not Run when I execute in the prompt ionic emulate iOS for example

Bah, you know what, I’m really sorry, I screwed up. I thought you were on Ionic 2! Damn. Although I’m pretty sure this describes your exact issue and has a solution: http://stackoverflow.com/questions/30950032/how-can-i-run-multiple-npm-scripts-in-parallel

So as per usual with npm there is lots of info out there, usually the answer is just a google search away.

Alternatively you could in your gulp file make a serve:before task, and then install the shell plugin and have gulp run it like:

gulp.task('serve:before', shell.task([
  'nodemon server.js'
]);

With that solution you could still run ionic serve instead of npm start. Either way one of these should get you waht you want.

1 Like

Almost worked ! see :slight_smile:

Well, you named your shell sh not shell, so it’s not really a surprise that shell is undefined.

Also, in my example I was using ‘gulp-shell’; not ‘shelljs’, either should work but the syntax may be different.

Well, now I install de gulp-shell, ok.

But now… Another mistake :frowning:

image

Well, you’ve got me on that one. Sounds like nodemon can’t start your server. Did you put the proper path to your server.js file? I’m guessing it’s not int he same folder as Ionic?

I believe so. The server is at the root. See the first print there at the beginning. Server.js is in the root.

hmm, well your gulpfile is in there too. Sorry this one is much harder for me to say. Maybe try nodemon ./server.js?

It’s in the same folder as your gulpfile from the looks of it, so I guess I’d think that would work? You could also try that solution from the stack overflow I posted. You could check what directory you are in when gulp runs by changing the shell script to ‘pwd’ to print the working directory, or to ls (or dir on windows) to see what files are in there. Just to confirm you’re in the right folder.

Hello !

I got it make the command work. :smile:

Well, when I run in the browser with the bash ‘IONIC SERVE’, everything works.

But when I run the bash command to emulate ‘IONIC EMULATE IOS’ my app opens normally, but does not work the login button that calls the server.js, which makes the connection with the mongodb.

Because I’ve been thinking … in the emulator and in a real mobile there is no bash in the system, or should it work anyway?

Ummm…what? I mean, if you have your app trying to hit your dataservice via localhost it should actually still work in the ios emulator. And ya of course emulators and mobile don’t have bash…but what does that matter, you’ll have your data service hosted somewhere…right…:confused:

Hello.

I think I asked the question wrongly.

In fact i want the server.js to be started as soon as i give the command ionic emulate ios or ionic emulate android

Okay so change it to emulate:before

well, not ran …

But let’s say it works, and now I want to make a release to go up in the google play store.

What should I configure ?

Because my application is ready. Just is not connecting with mongodb (because server.js is not being started).

:confused:

Can I send my application to you to have a look?

No. I think you lack a basic understanding of how apps and apis work. I thought you wanted to start both with one command as a convenience…it sounds like instead you think this is a requirement.

You have to host your node server and database somewhere. It’s totally separate from your app.

my database is on http://mlab.com

and is call by the server.js :

var db

MongoClient.connect('mongodb://elton:123456@ds035026.mlab.com:35026/star-wars-elton', (err, database) => {

  if (err) return console.log(err)
    db = database
    app.listen(3000, function() {
      console.log('mongodb  !!!');
      console.log('listening on 3000')
    })
});