Problem serving html form using middleware

Often times, I have issue locating files from root directory. I have made this simple register form, I serve html form files to user.js then serve the user.js to the main server.js using middleware function. i.e app.use(). I was hoping to see the link from the index.html display and finally post the form to be sure that I got the absolute path but to no avail. i have been struggling two days for this please help.
project link: Glitch

I didn’t look too much at your code but.

Example server.js

app.get("/", function(req, res) {
  res.sendFile(path.join(__dirname, "/views/index.html"));

You might also want to give the basic and advanced node and express sections of the curriculum a look.

1 Like

Hello lasjorg, your answer was so helpful i use it to make many things work in the main server that is //server.js engine . But this is now the basic problem am having:

  1. I don’t want the the template to be serve in the main script that is server.js on glitch,
    i want it to be served in //routes/user.js calling router.get();
const express = require("express");
const router = express.Router();
const path = require("path");

router.get("/", function(req, res){
  res.render(path.join("index", {talk: "hey", message: "hellow chris"}));

module.exports =  router;

Then using middleware that is, app.use() NOW in the main //server.js serve //routes.use.js then //app.use(,);

//importing packages from package.json  and modules from root directory 
const express = require("express");
const app = express();
const dbInitiation = require("./construct/dbs");
const bodyParser = require("body-parser");
const dotenv = require("dotenv").config({path: "./.env"});
const userRouter = require("./routes/user");
//const userRoute = require("./routes/user");
const path = require("path");
//set directory for views template files 
app.set('views', './views');
//set view engine
app.set('view engine', 'pug');
//serving mongodb 

app.use("/user", userRouter);

app.listen(process.env.PORT, function(req, res) {
  console.log("app running on port:", process.env.PORT);

In this case am seeing nothing; compare to when i work everything on //server.js
am hoping to see the message //hello chris

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.