How to host a Express web server on Discord Bot hosting
What is express? Well, Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Let's find out how to host a Express web server on Discord Bot hosting!
There is a video version of this article available. Click here to view the video.
Head over to your service and click "Startup".
Under the "Node Packages" box, add express to the list of packages. Just like so:
Restart the service to install the package
Express is easy to use and a lot of documentation is available for you to learn the ins and outs of the package. However, we will teach you how to setup a basic express web server.
Then create a file called .env. The contents of the file should like this:
Make sure to change the port to the port that is assigned as "Primary" on your service.
If you access the URL, the result should look something like this
If you require any further assistance, please create a ticket here.
Created By: Greg K
There is a video version of this article available. Click here to view the video.
Installing Express
Head over to your service and click "Startup".
Under the "Node Packages" box, add express to the list of packages. Just like so:
Restart the service to install the package
Using Express
Express is easy to use and a lot of documentation is available for you to learn the ins and outs of the package. However, we will teach you how to setup a basic express web server.
const express = require('express')
const app = express()
//This is a GET request route. It will be called when the user visits the root of the site.
app.get('/', (req, res) => {
res.send('Hello World! This is a GET request.')
})
//This is a POST request route. It will be called when the user submits data on the site.
app.post('/', (req, res) => {
res.send('Got a POST request')
})
//This sets the port that the web server will listen on. It will be the port specified in the .env file.
app.listen(process.env.SERVER_PORT, () => {
console.log(`Example app listening on port ${process.env.SERVER_PORT}`)
})
Then create a file called .env. The contents of the file should like this:
SERVER_PORT=25788
Make sure to change the port to the port that is assigned as "Primary" on your service.
If you access the URL, the result should look something like this
If you require any further assistance, please create a ticket here.
Created By: Greg K
Updated on: 10/07/2023
Thank you!