node.js – Unable to send an HTML file to frontend in Netlify using NodeJS
I am a little experienced with NodeJS, but I don’t understand most things anyway.
I want to host a NodeJS app using express using Netlify.
(I wanted to learn how to do it for my project, hence why this is very basic)
This is my server.js code:
const express = require("express")
const path = require("path")
const bodyParser = require("body-parser")
const app = express()
app.use(bodyParser.urlencoded({extended: false}))
app.get("/", (req, res) => {
res.sendFile(path.join(process.cwd(), "index.html"))
})
app.get("/js", (req, res) => {
res.sendFile(path.join(process.cwd(), 'index.js'))
})
app.get("/css", (req, res) => {
res.sendFile(path.join(process.cwd(), 'index.css'))
})
app.get("/data", (req, res) => {
res.sendFile(path.join(process.cwd(), "data.html"))
})
data = {}
app.post("/data/input", (req, res)=>{
let dataPkg = req.body
data[dataPkg.id] = dataPkg.name
})
app.post("/data/output", (req, res)=>{
res.send(data)
})
module.exports = {app};
This is my netlify.toml:
[build]
publish = "dist"
functions = "netlify/functions"
[functions]
external_node_modules = ["express", "path", "body-parser"]
[[redirects]]
force = true
status = 200
from = "/*"
to = "/.netlify/functions/index"
And, here’s how the directory looks like
directory img
I deployed the site using Netlify CLI, but when I try to open it, for any file, I get:
Error: ENOENT: no such file or directory, stat '/var/task/data.html'
What do I do?
(This is my first question on Stack Overflow)
Read more here: Source link
