I tried to change the JS code:
--8<--
console.log("Read server.js");
// debug require() => node_modules/ access
try {
const express = require("express");
console.log("require() OK");
const app = express();
app.get("/", (req, res) => {
res.send("Hello from OpenBSD");
});
app.listen(80, () => {
console.log("Server started on port 80");
});
} catch (error) {
console.log("require() failed!");
}
--EOF--
openbsd# rcctl start app
app(ok)
openbsd# cat /tmp/server.js.log
Read server.js
require() OK
Server started on port 80
openbsd# pgrep node
99648
openbsd# reboot
openbsd# cat /tmp/server.js.log
Read server.js
require() OK
Server started on port 80
openbsd# pgrep node
If I uninstall the express.js module: npm remove express
run: rcctl start app
openbsd# cat /tmp/server.js.log
Read server.js
require() failed!
I really don't understand why it doesn't work on reboot.
/Remi
Le 2023-09-27 23:42, Remi L. a écrit :
Thank you for your reply Lucas.
After the changes:
openbsd# rcctl start app
app(ok) -- it takes about 10 seconds to get it done
openbsd# pgrep node
76957
openbsd# cat /tmp/server.js.log
Server started on port 80
openbsd# rcctl stop app
openbsd# reboot
then:
openbsd# pgrep node
openbsd# cat /tmp/server.js.log
Server started on port 80
openbsd# pgrep node
openbsd# netstat -anf inet | grep 80
openbsd#
Same issue, I think it drop the require()... it continues, that's why
we get the Server started... message.
/Remi
Le 2023-09-27 23:26, Lucas a écrit :
"Remi L." <r...@sysdev.re> wrote:
Hello everyone,
I am an OpenBSD user and I recently installed Node.js. I managed to
create an rc.d script to start a basic Node.js application, but it
only
starts manually.
At OpenBSD boot, I can see that the application is started, but it
doesn't work. I have the impression that it is related to the
node_modules directory, because after inspection, the JavaScript file
seems to stop at the line :
const express = require("express");
It cannot load the express module.
Here are the full details so that you can reproduce the same error on
your side:
OpenBSD version: 7.3 (release + syspatch)
Node version: 18.15.0
npm version: 9.5.0
Let's create a basic node.js app:
mkdir /var/www/htdocs/app
cd /var/www/htdocs/app/
npm init -y
npm install express
touch server.js
chown -R www.www /var/www/htdocs/app
chmod -R 775 /var/www/htdocs/app
/var/www/htdocs/app/server.js file:
--8<--
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Hello from OpenBSD");
});
app.listen(80, () => {
console.log("Server started on port 80");
});
--EOF--
You can run `node server.js` to test the url: http://your-ip
Ctrl-c to quit
Now let's create the /etc/rc.d/app script:
--8<--
#!/bin/ksh
daemon="/usr/local/bin/node"
daemon_execdir="/var/www/htdocs/app"
daemon_flags="server.js"
daemon_logger="daemon.info"
After checking rc_exec() in /etc/rc.d/rc.subr, a hack you can try to
get
more information is removing the daemon_logger variable and change
daemon_flags to
daemon_flags="server.js >/tmp/server.js.log 2>&1
That should create a /tmp/server.js.log file with the program's stdout
and stderr. Give it a try with rcctl start / stop, and if it works, go
with a reboot.
#daemon_rtable=0
#daemon_timeout=30
#daemon_user=root
. /etc/rc.d/rc.subr
#pexp="$(eval echo ${daemon}${daemon_flags:+ ${daemon_flags}})"
rc_bg="YES"
rc_reload="NO"
#rc_reload_signal=HUP
#rc_stop_signal=TERM
#rc_usercheck= # (undefined or "NO")
#rc_configtest() {
#}
#rc_pre() {
#}
#rc_start() {
# rc_exec "${daemon} ${daemon_flags}"
#}
#rc_check() {
# pgrep -T "${daemon_rtable}" -q -xf "${pexp}"
#}
#rc_reload() {
# pkill -${rc_reload_signal} -T "${daemon_rtable}" -xf "${pexp}"
#}
#rc_stop() {
# pkill -${rc_stop_signal} -T "${daemon_rtable}" -xf "${pexp}"
#}
#rc_post() {
#}
rc_cmd $1
--EOF--
chmod +x /etc/rc.d/app
rcctl enable app
Start the app: rcctl start app
Check in the browser or use: netstat -anf inet | grep 80
Stop the app: rcctl stop app
Check that it is not anymore started: pgrep node => nothing
- Reboot -
If I do a `dmesg -s`, I can see the following line:
starting package daemons: app.
No errors, no output, even in the /var/log/daemon log.
I would be very grateful for any help or advice that you can provide.
Sincerely,
-- Remi
"Everyone makes mistakes, and the best way to learn is from our
mistakes." - Henry Ford
--
-- Remi