r/learnprogramming • u/Reasonable-Job4205 • 12h ago
How do I properly deploy this web server in production mode?
I set up a web server using https://github.com/seanpmaxwell/express-generator-typescript
I now need to deploy the production build to a remote server. I can generate the prod build with `npm run build` and then run it with `npm start` and it runs on my local machine. Is it possible to generate something that can be compiled before being sent over to the remote server? I'd like to run the web server in prod mode, but without putting the decompiled source code on the remote server. I don't want the source code over on that remote server because if the server gets compromised, I don't want anyone to be able to read the source code. New to web dev, so thanks for being patient.
2
u/BuildingShoddy8672 9h ago
Mechanical part first, because it changes the plan.
npm run build already gives you the program: TypeScript's types are erased, so the output is plain JavaScript and no build step makes it unreadable. Beyond that you have obfuscation with rising cost. Bundle and minify with esbuild or terser (a few minutes to undo for anyone who cares), compile to V8 bytecode with bytenode, or ship a single binary via pkg, nexe, or Node's own SEA support. Those stop casual reading, but the code can still be recovered off a machine you own, so treat them as speed bumps rather than protection.
What genuinely works is not shipping the logic. If a piece of code must stay secret, keep it on a service you control and have the remote server call it over HTTP.
The deployment pieces you are actually missing:
- Build locally or in CI and ship the built directory. Never run the build on the remote, that is also where dev dependencies would end up.
- Run it under a process manager instead of npm start in an open shell: a systemd unit or pm2, restart on crash, start on boot.
- NODE_ENV=production, listen on 127.0.0.1, and put nginx or Caddy in front for TLS so 80 and 443 are the only doors open.
- Secrets as environment variables or a .env file outside the repo (chmod 600), never baked into the artifact you ship.
- Run as a non-root user and firewall everything except 22, 80, 443.
Ranked by what will actually hurt you: leaked credentials and an unpatched OS come first, readable source a distant last.
2
u/Different_Pain5781 1h ago
TypeScript gets compiled into plain JavaScript before it runs on the remote machine. Just build it locally and upload the compiled files from your dist folder instead of shipping the raw source. Boot dev covers this kind of backend deployment stuff too.
2
u/Dameon_ 12h ago
First off, what do you think Typescript gets compiled to? Hint: It's a very readable language.
Second off, if there's anything in the source code that would compromise security if a hacker could read the source code, you're already in trouble. That's called security through obscurity, and it's not going to protect your server. If the server is compromised to the point where somebody's browsing through its local files, you're also already in trouble to an extent that hiding your source is not going to protect you.