Custom Domain for API

I’ve been trying to figure out a way to create an API with my custom domain that I bought for my game with express, but everything I’ve tried so far hasn’t been successful. I know HTML and Node.js, but am not very experienced with web development.

I want to put the site on my Digital Ocean server. I already know how to upload code and everything to the server and how to use express, and have my code for it already, but I don’t know how to set it up with my custom domain and make it usable instead of just having localhost://port.

If you are using a VPS, log into your DNS settings and make an A record for the IP of your server.

Thanks for replying. I had made an A record for the IP of the server already, but I’m just not sure how to utilize it in my code.

Can you explain this ^^^

To change it from http://localhost:port to my custom domain.

What happens when you visit your custom domain? If you custom domain works, just change localhost to your domain and the port to 80 (http) or 443 (https).

All I’m seeing is this. I don’t know why it says Namecheap though. I transferred it to Cloudflare.

Hmm, could still be propagating. How long ago was that?

30 minutes or so.

It might still need more time. Cloudflare will send you an email once the domain has transferred.

1 Like

@SS4PPHIRE

1 Like

I got the email almost directly after I changed the nameservers and didn’t notice. I’m still seeing the namecheap registration page on my domain though. These are the only DNS records besides the one pointing to my server’s IP address.

You should delete all of those records. I think the CNAME one is messing everything up.

That fixed it. I’m seeing this now though.

I’m using this as a placeholder for now and then I’ll upload the code I put on glitch once I ensure everything’s working fine.

const express = require("express");
const app = express();

app.use(express.static("public"));

const listener = app.listen(443, () => {
    console.log("app running")
});

app.get("/", (req, res) => {
    res.send("test")
});

I uploaded it to my server, but don’t know what to do next.

Is that image the error you got?

It’s what it shows when I visit the site.

Odd, could you show me your A record? You can also PM it to me.

americanocorporation.xyz redirects to google.com for me

Yeah. Same. I had added nameservers pointing to the ones for Digital Ocean, and now for some reason it’s redirecting to google which is super weird. No clue why.

It has done the same for every other domain I tried adding to Digital Ocean. (all of the DNS records are deleted)

Fixed. I had a couple of records for Namecheap that were for some reason redirecting to google. I still can’t use my site though, even after setting the port to what @RiversideRocks said to. I’m getting this.

I have one A record pointing to the IP of my droplet. This is the code I’m using:

const express = require("express");
const app = express();

app.use(express.static("public"));

const listener = app.listen(443, () => {
    console.log("app running")
});

app.get("/", (req, res) => {
    res.send("test")
});

Hey there, is seems like you records are messed up. Make an A record and make sure that you set the name as “@“ and set the target as the IP addressed the droplet. This should fix it. If you added “www” when connecting the domain, change the name to “www”.

2 Likes

I don’t know how custom domains work over there, I use Vultr cause it’s cheaper, but this had worked for me when I use Vultr.

1 Like

Thanks for the reply. This is what my DNS records look like on Cloudflare. americanoapi.xyz is pointed towards the IP of my droplet.

I’m not quite sure what you meant by this.

Actually, it might be an issue with the code. The PM2 process is erroring.


The folder where the code and packages are stored is called “api”.

Turn off the proxy. For instructions:

  1. Click on the record
  2. Click the orange cloud
  3. Click save.
1 Like

If that was the case, Cloudflare would send a status stating “the web server is down”

1 Like

Okay. Done. The error I’m getting is kind of nonspecific and I’m not sure what I need to fix.

The domain may not be fully transferred. Cause you would at least see a Cloudflare error.

1 Like

Cloudflare wouldn’t let me transfer the domain fully from Namecheap. It said I needed to wait 60 days before I could transfer. I could only edit the nameservers on the Namecheap dashboard to go to Cloudflare’s ones, and then Cloudflare sent me an email saying my site was activated.

Could this have anything to do with it?

This seems like a NameCheap problem more than a Cloudflare problem.

1 Like

Okay. I can afford buying another domain, since I need to get it set up asap. What domain registrar should I buy from?

Umm, I actually recommend GoDaddy. I only really have experience from them and Freenom. I never really used Freenom for production. Only downside to GoDaddy is, I found changing name servers a pain in the rear end. I was looking around for like 15 minuets before I decided to just look it up on their support page.

Thanks. I purchased one from GoDaddy and changed the Nameservers. I’m waiting for Cloudflare to detect that I pointed the Nameservers.

I restarted the Pm2 process on my ssh client, and still erroring even with the new domain, although I’m still not seeing anything in Cloudflare.

The screenshot shows running on port 8000, which doesn’t match your code above which is trying to listen to 443. Also the log shows EADDRINUSE. Try change your code to connect to port 8000

3 Likes

I noticed that, but it if you look closely, the first error is from Discord.js. And the second error is starting not null:80. Meaning that there is something wrong with how he is typing his code. Or a TypeError.

Also, port 80 is in use. Try doing pm2 stop all Then start it again.

I had actually changed it from 8000 originally to 443. I just forgot to change the log.

Changed port to 8000 and still getting the EADDRINUSE error unfortunately. I checked for availability on both port 8000 and 443 with sudo netstat -nltp | grep porthere, and neither are being used.

the parkingpage.namecheap.com is what that domain is.

Weird. I cleared all of the DNS records so I’m not sure why you would be seeing that. All I’m seeing is this on the page.

port should be 3000 for glitch, thats the port they use default. repl and others use something in the 80’s i believe

also, your domain just redirects me to google.

I honestly don’t know why that is. I cleared out all of the records and everything except for the A record pointing to the IP of my droplet. Could someone possibly have access to my Cloudflare account or something?

No one has access to your cloudflare most probably. Is your site on glitch or something else?

I’m using Digital Ocean for the VPS. The code that isn’t working is uploaded through Termius to the droplet.

@SS4PPHIRE I think I figured it out. I think that you are starting two instances of your app, or you are running app.listen() twice because your app mentions that it is running on port 8000, then it errors after that. If your code looks like this

app.listen(8000, ()=> console.log("running on port "+PORT))

Or something like that, it would return only that. But it seems you have it like this,

app.listen(8000, ()=> console.log("running on port "+PORT))
// listening again somewhere in your code.
app.listen(8000, ()=> console.log("running on port "+PORT))

So, that means there are two possible solutions. Make sure your package.json start script does not run it twice, and you are not running app.listen() twice.

Alternatively, you could use ps aux to look through the processes. Also, try going directly to the IP. If you are on Chrome, you may need to do this little trick. If you go there and there is no advance button, type this in no spaces, not on a text box. “thisisunsafe”. It should redirect you.

1 Like

I’m going to wipe my server and reinstall NPM and PM2. I may have messed something up beforehand. I’m not seeing app.listen twice, and there’s only one process, so I’m not sure.

Wiping it did the trick. I’m not getting any more errors. “App running on port 443”.

I’m still having the google issue though. I have no idea why it’s redirecting to google.

hmm. Did you put a 304 in your code?

I don’t know what that is, sorry. Pretty new to this.

A 304 status caches a redirect into the computer. So whenever you visit that endpoint, it will redirect you no matter what the code is.

I didn’t put anything like that in the code. Should I?

Don’t unless you are really good with Express, or if you really need it.

1 Like

Okay. What should I do to fix the google redirect?

Try clearing your cache, or try to log the requests.

Also, don’t append www to the begining of the domain. Just put the domain.

Okay. It looks like the can’t find IP address error is gone. I’m getting this now.


No more errors on Termius.

The droplet seems to be closing the connection.

At this point I would recommend you contact digital ocean support.

1 Like

Okay. I will. Thanks.

1 Like

I think it may be blocking the connection. But do what he said. They are the only ones who can fix it.

Got it working. I had to use port 80, and then run a command to unlock port 80. Thank you so much for the support!

For ports 8080 and 433 use https://.

In Cloudflare?

When typing in the domain.

Okay. Will do. Thanks. You helped me out a lot. Can’t thank you enough.

No problem.

on ur index.html or ssomewhere, does it specify “google.com”. try doing CTRL(CMD for mac) + f then google and it should show you. Keep doing that to all your files and you should find where its redirecting.

1 Like

It is an API.

1 Like

I had thought you unlocked it earlier but just realized you never did when you said your error I just kinda assumed in my subconscious. Never really crossed my mind :rofl:

2 Likes