尝试运行我的index.js文件,但无法正常工作

时间:2020-08-14 14:38:37

标签: node.js google-chrome-devtools

让我很想找出正确的文件路径。至少我认为这是文件路径错误。在Chrome中出现以下错误。 error1 error2

还在下面添加了我的index.js和app.js代码。我不明白为什么我的app.js mongodb运行正常?

---------app.js----------
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const app = express();

const mongoose = require("mongoose");




app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({
  extended: true
}));

mongoose.connect("mongodb://localhost:27017/userDB", {useNewUrlParser:true, useUnifiedTopology: true});

const Schema = mongoose.Schema;

const userSchema = new Schema({
  username: String,
  email: String,
  password: String
});

const User = mongoose.model("User", userSchema);

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

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

app.post("/register", function(req, res){
  const newUser = new User({
    username: req.body.username,
    email: req.body.email,
    password: req.body.password
  });

  newUser.save(function(err){
    if(err){
      console.log(err);
    } else{
      res.render("dashboard", {loggedInUser: req.body.username});
    }
  });

});

app.listen(3000, function(){
  console.log("server is running on port 3000");
});

-index.js ----

alert(“ test”);

1 个答案:

答案 0 :(得分:0)

您的两个src=""标签指向错误的位置。 app.js是一个目录, /ChatApp表示它指向/目录。

Br, 杜尔贡