I’ve beaten my head all weekend trying to find this. I’m new to Digital Ocean apps but very versed in other parts of DO. I may be going about this wrong entirely, but I can say that the below works just fine on localhost and in a droplet. This seems related to apps. Understand that the app deploys just fine, and the port spins up with node. My issue is the express 'app.get. routing.
Specifically this:
app.get("/", (req, res) => {
res.redirect('/assets/index.html');
when I hit the page in a browser with / it redirect to:
https://mydomain.com/assets/index.html
(Which is the correct path)
But I get the error: “Cannot GET /assets/index.html” in the browser. I can’t seem to manually traverse to any assets honestly beyond app.js which is in the root.
The structure of my folder structure (which I’m open to ideas) is:
-Apps
My Package.json is:
{
"name": "Test",
"version": "1.0.1",
"description": "The Website with submit form",
"main": "app/app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app/app.js"
},
"author": "ME",
"license": "ISC",
"dependencies": {
"@sendgrid/mail": "^8.1.1",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.18.3"
}
}
app.js is below. The HTML file really doesn’t matter. Its index.htmkl
const express = _require_('express');
const _bodyParser_ = _require_('body-parser');
const app = express();
const cors = _require_('cors');
const _sgMail_ = _require_('@sendgrid/mail');
//const path = require("path");
_require_('dotenv').config();
_console_.log(`Hello ${_process_.env.SENDGRID_API_KEY}`);
app.use(_bodyParser_._json_());
app.use(_bodyParser_._urlencoded_({extended: true}));
app.use('/assets', express._static_(_process_.cwd() + '/assets'));
app.use(cors());
app.get("/", (req, res) => {
res.redirect('/assets/index.html');
})
app.post('/formHandler', (req, res) => {
_console_.log('This the post function!!!! ', req.url);
_sgMail_.setApiKey(_process_.env.SENDGRID_API_KEY);
const msg = {
to: `${_process_.env.SENDGRID_TO}`,
from: `${_process_.env.SENDGRID_FROM}`,
replyTo: `${req.body.email}`,
subject: `${req.body.subject}`,
text: `${req.body.content}`,
};
_console_.log(msg);
if (req.body.email === undefined && req.body.subject === undefined && req.body.text === undefined) {
_console_.log('found bad packet');
} else {
_sgMail
_ .send(msg)
.then(() => {
_console_.log('Email sent');
//res.statusCode(200);
res.json({success: true})
res.send();
})
.catch((error) => {
//res.statusCode(400);
res.json({success: false, error: 'Bad'});
res.send();
_console_.error(error);
})
}
});
const PORT = _process_.env.PORT || 8080;
app.listen(PORT, () => {
_console_.log(`Server running on port ${PORT}`);
});
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Heya,
If I get this correctly you are having issues serving the static files, correct?
Please give the following docs a read and see if that would help out:
https://docs.digitalocean.com/products/app-platform/how-to/manage-static-sites/
Additionally, it’s weird to have your index file in the assets file, can you try putting it anywhere else just to see how it goes?