记录一下,学习express的小成就

终于搞出来了mongoose 和express 前后端链接的部分。

主要目的是为了使用markdown转换网页。

项目随便写的。没有参考价值,在此只是为了做个记录。作为学习的一个里程碑。对于nodejs,终于可以自己探索,也算是入门了吧。

各位观众不要看了。这个文档非常的粗糙,以后还会出更条理化的。

构建node项目。

我的目录是 \model \views,就这两个,\model 下是db.js

const mongoose = require("mongoose");
mongoose
    .connect("mongodb://localhost/testNoBB")
    .then(() => console.log("数据库lianjie 成功"))
    .catch(() => console.log("数据库链接失败"));

contentSchema = new mongoose.Schema({
    content: String,
});

Content = mongoose.model("Content", contentSchema);

module.exports = { Content };

\views下是网页代码,下面有两个网页文件 inputForm.ejs 和 output.ejs

inputForm.ejs

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <form action="http://localhost:3000/Input" method="get">
    <textarea name="content" id="" cols="30" rows="10"></textarea>

    <input type="submit" name="submit" value="tijiao">
  </form>

</body>

</html>

output.ejs

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>

<%- content %>

</body>
</html>

根目录下有http.js文件,

http.js

const express = require("express");
const app = express();
require("./model/db.js");
const { Content } = require("./model/db.js");

const { marked } = require("marked");

app.set("view engine", "ejs");
app.engine("html", require("ejs").renderFile);

app.get("/", function (req, res) {
    // res.writeHead(200, { "Content-Type": "text/html" });
    res.send("Hello world <br>");
    // res.send("hte world is beaytiful");
    // res.end();
});

app.get("/Input", (req, res) => {
    console.log(req.query.content);

    Content.create({
        content: req.query.content,
    });

    res.send("the form is received");
});

app.get("/md", (req, res) => {
    var html = marked(
        "# Marked in Node.js \n \n ## Rendered by \n\n  ### marked  \n \n  for test  ."
    );
    res.send(html);
});

app.get("/list", async (req, res) => {
    let result = await Content.find({}).exec();
    // res.send(result);
    // console.log(typeof result);
    console.log(result[result.length - 1].content);
    let resultContent = result[result.length - 1].content;
    var content = marked(resultContent);

    res.render("output", { content });
});

app.get("/abc", (req, res) => {
    res.render("inputForm");
});

// app.get("output", (req, res) => {
//     render("output", { content });
// });

app.listen(3000);

没有注释啊。

运行 

nodemon app.js

打开 localhost

 直接输入markdown文件,就可以了。

渲染之后的网页是这样的。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值