我有一个index.js文件,用于将表单中的数据导入MongoDB
const express = require("express"); const app = express(); const mongoose = require("mongoose"); const bodyParser = require("body-parser"); app.use(bodyParser.urlencoded({extended: true})); mongoose.connect("mongodb+srv://", { useNewUrlParser: true}, {useUnifieldTopology: true}) // create a data schema const dataSchema = { doc: String, email: String, feedback: String } const info = mongoose.model("Info", dataSchema); app.get("/", function(req, res) { res.sendFile(__dirname + "/index.html") }) app.post("/", function(req, res) { let newinfo = new info({ doc: $('#fileid').val(), //doc_val, //req.body.file, email: $("#emailid").val(), //email_val, //req.body.email, feedback: $("#feedback").val() //req.body.feedback }); newinfo.save(); }) //app.post app.listen(3000, function() { console.log("server is running on 3000") })
但不断出现Uncaught ReferenceError: express is not defined
错误我尝试过:
- 从package.json中删除
{ "Type": "module"}
,它不起作用,得到同样的错误 - 使用
import express from 'express';
而不是require
,但得到Uncaught SyntaxError: Cannot use import statement outside a module
- 将
<script type="module" src="node_modules/express/index.js"></script>
添加到index.html,仍然不起作用 - 将
<script type="module" src="./script.js"></script>
添加到index.html以将import { express } from './node_modules/express/index.js';
存储到脚本中。js - 添加
webPreferences: { nodeIntegration: true; contextIsolation: false}
这也不起作用。
我的index.html上的表单还连接到一个flask应用程序,因此index.js还包括其他功能。
如有任何建议,我们将不胜感激!