express.js(node.js)——自己能够独立理解公式教程的难度较大

请解释这个问题。
请说明这个问题。
请阐述这个问题。
请澄清这个问题。
请说明这个疑问。

我发现 Express 的教程有很多基础解释,阅读起来很困难,我做不下去了。我更希望能够边动手边操作来运行它。

当着力于理解难以理解的事物。这就是工程师的本职工作。
一边告诉自己这样的话。

环境

    express 4.16.1
    npm 9.6.7
    MacOS

准备

假定 npm 已经安装。

使得能够使用express命令

npm install express-generator -g

创建项目

以下命令可以在指定目录(express-locallibrary-tutorial)中创建一个express项目。

express express-locallibrary-tutorial --view=pug
cd express-locallibrary-tutorial
npm install

启动服务器

 DEBUG=express-locallibrary-tutorial:* npm start

网络访问

请访问 http://localhost:3000/

在用于索引的模板文件中尝试修改文字

请以中文进行本地化规则,只提供一个选项:
查看 /索引.pug

extends layout

block content
  h1= title
  p Welcome!!!!!!!!!! to #{title}
image

尝试更改index.js中的消息。

只是文件更改并不会改变页面的显示。
只有在服务器重新启动后才会反映更改。

在运行服务器的控制台选项卡上按下 Ctrl + C 等停止后,再次运行 npm start。

路由/index.js

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Expresssssss' }); // このメッセージを変える
});

module.exports = router;
image

创建数据库

公式教程从这里开始变得特别难懂。
(https://developer.mozilla.org/ja/docs/Learn/Server-side/Express_Nodejs/mongoose)

我完全看不到应该把什么写在哪个文件里。
是要写作为express代码的数据库相关内容,还是仅仅写node.js单独的文件。

安装MongoDB

由于 Express 的默认数据库是像 MongoDB 这样的,所以需要在本地安装它。

由于安装可能不是一件简单的事情,所以根据环境可能需要付出一些努力。
需要以某种方式进行安装和启动。

若是指的是Mac平台

安装MongoDB本体

brew tap mongodb/brew
brew install mongodb-community

启动DB服务器

brew services start mongodb-community

猫鼬

安装

这似乎不是DB本体,而是用于操作DB的库。

使用npm安装mongoose

确认动作

参考Mongoose官方文档编写了Node脚本,并成功保存了数据。

指令示例

运行 example.js 的节点。

文件示例


// https://mongoosejs.com/docs/index.html

const mongoose = require('mongoose')

main().catch(err => console.log(err))

async function main() {
  await mongoose.connect('mongodb://127.0.0.1:27017/test')

  // スキーマの定義
  const kittySchema = new mongoose.Schema({
    name: String
  })

  // モデルにスキーマをコンパイルする (らしい)
  const Kitten = mongoose.model('Kitten', kittySchema)

  // DBに保存する
  const fluffy = new Kitten({ name: 'fluffy' })
  await fluffy.save()

  console.log('SAVE')

  // 特定の条件式でデータを取得する
  // DBへの保存なのでこのスクリプトを起動するたびにデータが増える

  // 表示例
  // [
  //   {
  //     _id: new ObjectId("64e9f4bb6576f87bdc838c0e"),
  //     name: 'fluffy',
  //     __v: 0
  //   },
  //   {
  //     _id: new ObjectId("64e9f4c7e16502c2c740eb72"),
  //     name: 'fluffy',
  //     __v: 0
  //   }
  // ]
  const kittens = await Kitten.find({ name: /^fluff/ });
  console.log(kittens)

  // レコード個数を表示
  const count = await Kitten.count()
  console.log(count)
}

在页面访问时尝试进行数据库保存。

每次访问 http://localhost:3000/ ,尝试将其保存到数据库中
(如果有更改,需要重新启动服务器)

路由/index.js

const mongoose = require('mongoose')

// https://mongoosejs.com/docs/index.html


mongoose.connect('mongodb://127.0.0.1:27017/test')

// スキーマの定義
const kittySchema = new mongoose.Schema({
  name: String
})

const Kitten = mongoose.model('Kitten', kittySchema)

var express = require('express');
var router = express.Router();

router.get('/', function(req, res, next) {
  // DBに保存する
  const fluffy = new Kitten({ name: 'fluffy' })
  fluffy.save()

  res.render('index', { title: `Expresssssss` });
});

module.exports = router;

如果进行这样的操作,每次页面访问时都可以将记录保存到数据库中,看起来是可以的。
只要运行之前的node脚本,就能知道数据库中的记录数。

要如何将从数据库中获取的数据显示在屏幕上?

我对此仍不甚了解。

即使试图直接显示find的结果,也是不可能的。
以后如果心情好的话会继续尝试。

可以作为参考

招募聊天成员

如果您有任何问题,烦恼或需要咨询,请随时使用LINE开放聊天功能。

推特

bannerAds