我正在尝试使用youtube视频将Web应用程序与数据库连接,并且当前构建数据库连接javascript文件以从表单提交并存储在数据库中。我想知道这个预期的javascript文件链接到代码的主要部分以进行编译和执行。此外,我们在哪里可以观察应用程序后端部分的输出。请帮助我摆脱这个
Youtube link: https://www.youtube.com/watch?v=ZKwrOXl5TDI
Program code for database connection is attached here
var express = require('express');
var router = require.Router();
var mongo = require('mongo');
var assert = require('assert');
console.log("testing");
var url = '';
/* GET HOME PAGE*/
router.get('/', function(req, res, next)){
res.render('index', { title:'tournament', condition: true, anyArray: [1,2,3] });
});
/*getting data in the database*/
router.get('/get-data', function(req, res, next){
var resultArray = [];
/* connection of database to the form*/
mongo.connect(url, function(err, db) {
assert.equal(null, err);
var cursor = db.collection('user-data').find();
cursor.forEach(function(doc, err) {
assert.equal(null, err);
resultArray.push(doc);
}, function() {
db.close();
res.render('index', {items: resultArray});
});
});
});
router.get('/insert', function(req, res, next){
var item = {
title: req.body.title,
content: req.body.content,
author: req.body.author
};
/* connecting the database*/
mongo.connect(url, function(err, db) {
assert.equal(null, err);
db.collection('user data').insertOne(item, function(err, result)) {
assert.equal(null, err);
console.log("data inserted");
db.close();
});
});
res..redirect('/');
});
/*updating the database*/
router.get('/update', function(req, res, next){
});
/*deleting the database*/
router.get('/delete', function(req, res, next){
});
module.exports = router;
package.json file is attached here:
{
"name": "Tournament",
"version": "0.0.1",
"description": "Tournament",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"keywords": [
"react",
"tutorial",
"donation",
"example"
],
"author": "zenva",
"license": "ISC",
"dependencies": {
"body-parser": "^1.14.1",
"express": "^4.13.3",
"mongodb": "^3.4.3"
}
}