简体   繁体   中英

Running web-server from inside chrome app

I'm very much new to chrome app development, for my application I need to run a web-server which will start as my app launch and will be close with closing of my app. I like to use web-server-for-chrome plugin as my web-server but is there any way to start and stop a server from inside chrome app?

Sure, you would need to use chrome.sockets.tcpServer api , it allows you to create server applications using TCP connections.

And for examples, take a look at chrome-app-samples-webserver .

You can use web-server-chrome for this.

As you mention, it's important to stop the web server so that the app's background or event page can suspend. You could create an chrome.app.window.onClosed handler and check if there are no more windows, and then call .stop() on the WebApplication object.

Thanks Haibara for putting me in the right direction. I have actually made a small library from your advice which answers the original question.

https://github.com/markwylde/https-server

const https = new ServerHttps()

https.get('/', function (req, res) {
  res.send('hello there')
})

https.listen(9999)

It exposes an express like api, which you can run as an extension.

A better demo can be seen here: https://github.com/markwylde/https-server-demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM