简体   繁体   English

如何使用 NodeJS 提供多个 html 文件

[英]How to serve multiple html files using NodeJS

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.我用 nodeJS 创建了一个 mongoDB 表单,我可以提供注册页面,但是当我点击 go 到其他页面的任何其他链接时,它会抛出“Cannot GET /index.html”。 I am unsure of how to serve multiple files using nodejs.我不确定如何使用 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);
});

node.js by itself serves no content at all. node.js 本身根本不提供任何内容。 So, any URL that you want your server to respond to must have a corresponding route in Express.因此,您希望服务器响应的任何 URL 都必须在 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.或者,您可以使用express.static()中间件为位于目录层次结构中的一组 static 页面提供服务。

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知道哪些是 static HTML 页面与动态
  3. Know where they are located in your local file system知道它们在本地文件系统中的位置
  4. See what URLs you want to be used for each page查看每个页面要使用的 URL

You can see a general tutorial for express.static here and here .您可以在此处此处查看 express.static 的一般教程。

when I click on any other links to go to other pages, it throws "Cannot GET /index.html" for example.例如,当我单击 go 到其他页面的任何其他链接时,它会抛出“Cannot GET /index.html”。 I am unsure of how to serve multiple files using nodejs.我不确定如何使用 nodejs 提供多个文件。

You will need to teach your node.js server how to respond to those other links.您需要教您的 node.js 服务器如何响应其他链接。 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.为每个页面创建一个自定义路由,或者对于 static 页面,使用express.static()以便它可以使用一行中间件为一堆 static 页面提供服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM