3
var express = require('express')()
const app = express();
var port = {
  'client_id': '…',
  'client_secret': '…',
  'grant_type': 'authorization_code',
  'redirect_uri': '…',
}

app.get('/', (request, response) => {
  return response.sendFile('index.html', { root: '.' });
});

app.listen(port, () => console.log(`App listening at http://localhost:${port}`));
console.log('1')

How do I use Discord Oauth2 for GitHub Pages? It says that require is not defined. And even on local hosting, it says that I had to wrap the strings in array in require('express'). Then, it says that app.get is not a function.

And after all of this, how do I make a request to retrieve the servers of the person logged in?

John
  • 87
  • 4
  • 4
    Um... GitHub pages is static. No server side allowed. – no ai please Dec 18 '21 at 03:38
  • 1
    also, it's not require("express")(), its require("express") – MrMythical Dec 18 '21 at 03:39
  • @MrMythical Actually, not quite - `const app = require('express')()` is the equivalent of doing something like `const express = require('express'); const app = express();`, the latter being what you see in the official docs. Both work just the same. – esqew Dec 18 '21 at 03:41
  • Does this answer your question? [Client on Node.js: Uncaught ReferenceError: require is not defined](https://stackoverflow.com/questions/19059580/client-on-node-js-uncaught-referenceerror-require-is-not-defined) – no ai please Dec 18 '21 at 03:42

1 Answers1

3

GitHub Pages is a statically hosted hosting service, whilst Express is a server-side framework server.

GitHub Pages is a static site hosting service that takes HTML, CSS, and JavaScript files straight from a repository on GitHub, optionally runs the files through a build process, and publishes a website.

Source

This means that you cannot use Express with GitHub Pages. However, there are workarounds and alternatives.

Workaround: Load content client-side

This is the recommended route if you want to stick to GitHub Pages. You would have a static skeleton page given to the user and fill in the fields using JavaScript on the client.

There are multiple approaches to this method, however, it depends on how you are handling authentication.

Alternative: Use other hosting services

I assume that you are using GitHub Pages because it is a free hosting service, but the catch is that it's static only.

Both Vercel and Netlify have something called serverless functions. With serverless functions, a new computing instance is spun up every time a person makes a request to your website. Here are some official guides on how to implement Express:

Good luck with your project!

GodderE2D
  • 326
  • 2
  • 10
  • 1
    This time, another error occurred: `app.get is not a function` I tried `require(['express'])` and optionally added `()` at the back, but it didn't work either. – John Dec 18 '21 at 11:56
  • @John what method are you using? client-side rendering, or using Vercel or Netlify? – GodderE2D Dec 18 '21 at 17:03
  • 1
    Vercel. Thanks! – John Dec 18 '21 at 17:13
  • It's `require('express')` without the `[]`s. And make sure you exported the app at the bottom of your code, `module.exports = app`. For more instructions, see the [guide](https://vercel.com/guides/using-express-with-vercel#standalone-express). – GodderE2D Dec 18 '21 at 17:48
  • Oh and also, you should use either one of the below code. Not both. `const express = require('express'); const app = express();` or `const app = require('express')();` – GodderE2D Dec 18 '21 at 17:49