chirag chirag - 10 days ago 6
Node.js Question

start and stop expressjs app of node.js using php

I am making a web service, for that I am using express.js of node.js npm.

Wow to submit this project to my client I need to make a controller file to start & stop server without using command prompt in server.

I am using express.js so, to start server, I am using

set DEBUG=myapp:* & npm start
command because my server is Linux based. I am also very well in php so is there any way to just start & stop my server without using command prompt or terminal in server?

I already referred to this link
but, can't understand how to use it for express.js framework. I mean using this file i can only run individual node files cannot run
set DEBUG=myapp:* & npm start


Please help...

Even if there is another way instead of this php option then also it will be helpful for me.

Answer

Update

  • first install express.js properly How to install express.js from here
  • see if reponse is this then its express install globally and work fine

  • type express in CMD if see below response then its mean it install correctly and now u call them out side of directory express

  • create your project using express and run here that its working or not then stop it run your php code
  • snapshot shows how to make project and run here is my express project
  • after running you will see the following screen

express - booom now you are ready start your server from php - here node server response enter image description here - my php screen output php response

now php code will be for express app

`exec('set DEBUG=server.js:* & npm start');`

    echo "server run sucessfully";

Note: do not write app.js write server.js

  • keep app.js in same directory where your php file is
  • make app.js file

var http = require('http');
const PORT=9999; function handleRequest(request, response){ response.end('app.js start from php'); } var server = http.createServer(handleRequest); server.listen(PORT, function(){ });

you php file

//for starting
  exec('node server.js');
echo "app.js run sucessfully";}
//for stoping 
    exec('taskkill /f /im node.exe')
        echo "all stoped node.exe instances";

Note you can kill your app.js by PID

Now visit the url you will see the app.js is running

http://127.0.0.1:9999/