How to host Node.Js server and PostgreSQL database

2019-09-11 05:18发布

I want to host my own server and database on my computer, I don't want to pay monthly for services. I developed a node.js app and it's using a postgresql database. I have a domain with an angular app and the app needs to use data from the server.

Can someone tell me how I can do this and which OS would be the best?

Thanks!

1条回答
Deceive 欺骗
2楼-- · 2019-09-11 05:47

You have to do few things for that to work.

  1. First, your Angular app needs to be able to connect with your home server so it either needs a static IP address accessible from the outside, a dynamic IP with dynamic DNS, or a VPN.
  2. Your server needs to properly support CORS so that your Angular app would be able to connect with it. It will send OPTIONS requests that your server needs to handle properly.
  3. Make sure that your server is always on, the internet connection is reliable, the power is reliable and that your services are properly restarted on reboot.
  4. Make sure that your server is always up to date with security patches, is configured properly and doesn't use any unneeded software and services.

For (1) you have a lot of options and it all depends on whether you have a static or dynamic IP address, whether it is accessible from the internet etc. which you didn't include in your answer.

For (2) it depends on what Node framework do you use for you server-side application which you didn't include in your question. You need to use a way to set up CORS that is specific for the framework that you use.

The (3) is hard in home environment but it's important because on any downtime your users will not be able to use your application.

The (4) is critical in home environment because if anyone breaks into your server, he'll have access to your home network which may have a different kinds of consequences that breaking into a data center.

Another option would be to use a cheap VPS provider like Digital Ocean where you can get a server for $5 a month (or 2 months for free with this link) which may be less hassle that setting up your own server - for which you have to pay for electricity, manage the hardware, monitor the connectivity etc.

If you choose a VPS then (1) us taken care for you - you get your own static IP address accessible from the world, (3) is taken care for you completely, (4) is relatively easy to do and the biggest issue is making sure that CORS works as it should - but here you can host your API on the same domain as your frontend and then you don't need to worry about CORS at all.

If you get a VPS then you can host your frontend Angular app from the same server so that it doesn't even have to cost you more.

查看更多
登录 后发表回答