Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

express - Node.js with A2 Hosting Error 404 when loading mydomain.com/anyotherpage

I have followed the steps on A2's tutorial for "Cannot GET" URL here: https://www.a2hosting.com/kb/cpanel/cpanel-software/node-js-application-error-message-cannot-get-url

The main page, mydomain.com, loads fine and the CSS loads as well but when I try to go anywhere else, like mydomain.com/port or mydomain.com/anythingelse, I get a 404 error (pic below).

The ejs and CSS templates are in the same file location, and if I change the "/" route to the other page's .ejs/.css that loads just fine so it is finding those routes, but for some reason no other URL routes besides "/" load.

mydomain.com - loads perfectly fine

mydomain.com/anythingelse - the 404 error

This is my app.js:

const express = require("express")
const app = express()
const path = require("path")

const cors = require('cors')
app.use(cors())

app.set("views", path.join(__dirname, "views"))
app.set("view engine", "ejs")

app.use("/test1/static", express.static("public/"))

app.get("/", function (req, res) {
  res.render("home-visitor")
})

app.get("/port", function (req, res) {
  res.render("port")
})

app.listen()

I have kept my Applicaiton URL blank on purpose so I have a clean home page address

I have tried using a non-blank Application URL and change the app.js file to match syntax accordingly with no difference in outcome (tried '/test/port/', '/test/port', 'test/port/', 'test/port'

This is what I find in the console under Network>post>Headers when I try to load the page on mydomain.com/port

Thanks for any help, I am still new to node.js being hosted with A2 Hosting so I am hoping I am missing something simple.

question from:https://stackoverflow.com/questions/65837567/node-js-with-a2-hosting-error-404-when-loading-mydomain-com-anyotherpage

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your app.JS you have

app.set("views", path.join(__dirname, "views"))
app.set("view engine", "ejs")

That means that if you do something like res.render(“port”)

inside of your views folder you need to have a port.ejs file

Take a look at one of my old repos https://github.com/EnetoJara/KardexDL/tree/master/views/template

The code is a lil bit out of date but you can get some ideas


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...