Hi guys, I recently started using glitch and was trying to create a node server from my GitHub repo but for some reason I can’t get it to run. Can someone help me figure out what’s wrong?
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
index.html
isn’t in the same directory as index.js
- can you find the path to index.html?
index.html is in a folder ‘public’ which is in the same directory as index.js. Therefore I’m doing
app.use(express.static(__dirname + 'public'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
Hmmm - I just see “starting…” forever. This seems like a phaser.js game - could you try editing the HTML file so it just says “hello world” or something?
It might work better if you use this:
app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
or, you could try doing just this and have all your files in the public folder:
app.use(express.static('public/')); // if that does not work, remove the slash
I have a few beginner question, I hope you don’t mind.
Once I have all the server code implemented, do I have to start the node server myself from the console before running the project by executing:
node index.js
in the terminal?
And if yes which all ports am I allowed to use?
You need to create a package.json
file - see https://docs.npmjs.com/files/package.json, and in start put node public/index.js
.
Glitch only supports port 3000.
Thank you.
In my index.js, I have:
server.listen(8081, function () {
console.log(`Listening on ${server.address().port}`);
});
So I need to change 8081 to 3000, right?
Yes, that is the port Glitch supports.