1

I created a mongoDB form with nodeJS and I can serve the signUp page, but when I click on any other links to go to other pages, it throws "Cannot GET /index.html" for example. I am unsure of how to serve multiple files using nodejs.

var express = require("express");
var app = express();
var port = 3000;
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

var mongoose = require("mongoose");
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost:27017/ClubArchive_Users");
var nameSchema = new mongoose.Schema({
     fname: String,
     lname: String,
     email: String, 
     uname: String,
     pwd :String
});
var User = mongoose.model("User", nameSchema);

app.get("/", (req, res) => {
    res.sendFile(__dirname + "/SignUp.html");
});

app.post("/addname", (req, res) => {
    var myData = new User(req.body);
    myData.save()
        .then(item => {
            res.sendFile(__dirname + "/index.html");
        })
        .catch(err => {
            res.status(400).send("Unable to save to database");
        });
});

app.listen(port, () => {
    console.log("Server listening on port " + port);
});
1
  • where is your code which will return html file for other links? Commented Nov 15, 2019 at 23:41

1 Answer 1

2

node.js by itself serves no content at all. So, any URL that you want your server to respond to must have a corresponding route in Express. You can code each of them individually with

app.get(someURLPath, someHandler);

Or, you can serve a group of static pages located in a directory hierarchy using express.static() middleware.

To help you more specifically, we'd need to:

  1. See the overall list of pages you want to serve
  2. Know which ones are static HTML pages vs. dynamic
  3. Know where they are located in your local file system
  4. See what URLs you want to be used for each page

You can see a general tutorial for express.static here and here.

when I click on any other links to go to other pages, it throws "Cannot GET /index.html" for example. I am unsure of how to serve multiple files using nodejs.

You will need to teach your node.js server how to respond to those other links. Either create a custom route for each one or, for static pages, use express.static() so that it can serve a bunch of static pages with one line of middleware.

Sign up to request clarification or add additional context in comments.

4 Comments

ok awesome, lets say I have a dynamic webpage, and I have about 6 other HTML files, could I just route them individually? What is the code to do so?
@ArvindVenkatesan - Routing them individually works just like your "/" route you have right now that serves "/SignUp.html".
Ok! Thanks so much!
@ArvindVenkatesan - Since it looks like you may be new here, if this answered your question, you can indicate that to the community by clicking the checkmark to the left of the answer. That will also earn you some reputation points here on stackoverflow for following the proper procedure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.