I made a subtle modification to the original repository to achieve these three functions. It's worth noting that GPT, especially GPT-4, has done much more for me than a simple Google search. I saved the conversation file here for interested people.
We need to use the IP (e.g., 64.69.41.183:3000) instead of localhost (e.g., localhost:3000) to allow accessing the server's webpage through a local browser. Specifically, it's crucial to modify the code in the package.json file.
--------before-------------------------------
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
"start": "sirv public --single",
"start:dev": "sirv public --single --dev --port 3000",
"deploy": "npx"
}
------after-------------------------------------------
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
"start": "sirv public --single --host 0.0.0.0",
"start:dev": "sirv public --single --dev --host 0.0.0.0 --port 3000",
"deploy": "npx"
}
It's necessary to ensure that the process remains alive even if we shut down the terminal. Some simple instructions don't work here, such as nohup npm run dev > output.log 2>&1 &
. I adopted a more complex method as follows:
/etc/systemd/system
, create a file your-app.service
(your-app
is your application's name):sudo nano /etc/systemd/system/your-app.service
your-app.service
(<your-user>
is the username (e.g., root), and <your-app-path>
is your application's path):[Unit]
Description=Your App Service
After=network.target
[Service]
User=<your-user>
WorkingDirectory=<your-app-path>
ExecStart=npm run dev
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
your-app.service
. And reload systemd
:sudo systemctl daemon-reload
sudo systemctl start your-app.service
sudo systemctl enable your-app.service
sudo systemctl status your-app.service