I'd like a user to be directed to a specific page (promo1.html, promo2.html, etc...) when a corresponding promo code is entered into the form field. This runs nicely when I use it on localhost: but after deploying to Netlify it gives me a 404 when I enter a code.
I've tried everything I can think of, is there something wrong with my code or is this a Netlify formatting/routing issue and how do i resolve it?
https://github.com/extrajake/telus_distributor
It's ok that the promo codes are hardcoded in the file for the moment, I'll deal with making that dynamic later.
const express = require('express');
const serverless = require('serverless-http');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
// Static files
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.post('/submit', function (req, res) {
const codes = ["1NKCI", "V3L7R", "60HPC", "8SF6L", "AHG08"];
const codes2 = ["2WEMV", "KWWNI", "XNB7X", "6BN2T", "Y2FOK"];
const codes3 = ["YJYZL", "6HNP2", "V9FF8", "8SF5L", "5B23I"];
const codes4 = ["UVUNU", "1N5UL", "IINVK", "AEZNP", "HM2NM"];
const codes5 = ["KGFIA", "XI28K", "2QQ8K", "9Y4HK", "DMQJT"];
if (codes.includes(req.body.field)) {
res.redirect('/promo1.html');
} else if (codes2.includes(req.body.field)) {
res.redirect('/promo2.html');
} else if (codes3.includes(req.body.field)) {
res.redirect('/promo3.html');
} else if (codes4.includes(req.body.field)) {
res.redirect('/promo4.html');
} else if (codes5.includes(req.body.field)) {
res.redirect('/promo5.html');
} else {
res.send('The code is invalid');
}
});
module.exports.handler = serverless(app);
neltify.toml
[build]
functions = "functions" # Adjust this directory name if needed
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
I've tried adding redirects to netlify.toml, placing npm run build in Netlify app and moving my app.js file in public, src, functions and root, nothing.
Here's the page: https://main--musical-manatee-2e343b.netlify.app