Giter Site home page Giter Site logo

express_blog's Introduction

使用Express搭建一个多人博客

参考地址:https://github.com/nswbmw/N-blog 完成一个完整的项目流程

记录学习到的知识点,取人之长 补己之短

项目基础知识准备阶段

require这个commonjs的模块规范,其加载过程是同步的,也就是不能异步输出模块或者对象

  • require目录的机制是:
    • 如果目录下有package.json并指定了main字段,则用之
    • 如果不存在package.json,则依次尝试加载目录下的index.jsindex.node
  • require过的文件会加载到缓存,所以多次require同一个文件(模块)不会重复加载
  • 判断是否是程序的入口文件有两种方式:
    • require.main === module(推荐)
    • module.parent === null

如果发生了循环引用,导致未准备好的模块返回了一个空对象,然后就容易报undefined is not a function这个错误

npm新学到的:

  • npm config set命令将配置写到了 ~/.npmrc 文件,运行npm config list可以查看
  • 为了锁定当前依赖的版本呢,除了save-exact之外,我们可以把内层依赖版本一并锁定要使用npm shrinkwrap
  • 生成的npm-shrinkwrap.json里面包含了通过node_modules计算出的模块的依赖树及版本
  • npm shrinkwrap 只会生成dependencies的依赖,不会生成devDependencies

项目框架的前备知识

在开发过程中,每次修改代码保存后,我们都需要手动重启程序,才能查看改动的效果。使用supervisor可以解决这个繁琐的问题

express路由基本知识:

req.query: 解析后的url中的querystring,如?name=hahareq.query的值为{name: 'haha'} req.params: 解析url中的占位符,如/:name,访问/hahareq.params的值为{name: 'haha'} req.body: 解析后请求体,需使用相关的模块,如body-parser,请求体为{"name": "haha"},则req.body{name: 'haha'}

当确定了使用ejs模板后,需要在index.js中设置存放模板文件的目录和设置渲染的模板引擎

ejs模板的基本语法:

  1. <% code %>:运行JavaScript代码,不输出
  2. <%= code %>:显示转义后的HTML内容
  3. <%- code %>:显示原始HTML内容
  4. 使用includes来完成模板的精确拆分和组合~

PS: 当 code 比如为

hello

这种字符串时,<%= code %> 会原样输出

hello

,而 <%- code %> 则会显示 H1 大的 hello 字符串。

搭建真正的博客项目

教程:https://github.com/nswbmw/N-blog 进阶参考:https://github.com/ZJH9Rondo/Blog-Node

ESLint的配置流程:

  1. 电脑上全局安装eslint: npm i eslint -g
  2. 运行eslint --lint,初始化配置,依次选择-> Use a popular style guide -> Standard -> JSON
  3. eslint会创建一个.eslintrc.json的配置文件,同时自动安装并添加相关的模块到devDependencies
  4. VS Code需要装ESLint插件

EditorConfig配置方法:

  1. VS Code需要装一个插件:EditorConfig for VS Code
  2. 在项目根目录下新建.editorconfig文件,并添加格式内容

内容样例:

# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2 # 使用两个空格缩进
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true # 在代码最后自动插入一个空的换行
tab_width = 2

[*.md]
trim_trailing_whitespace = false  # 不删除每一行多余得空格

[Makefile]
indent_style = tab

针对路由的设计应该都作为注释写到routes/index.js中,方便维护和代码编写。 注意一下ejs模板里面的变量,虽然最后都是挂载到res.local上,blog变量挂载到app.locals下,将usersuccesserror挂载到res.locals下 在express框架中最后还是要把上面说的都merge到模板中,从而可以直接使用,但是优先级是res.locals>app.locals, 前者是每次请求都可能有变化的量

express_blog's People

Watchers

 avatar  avatar

Forkers

lizhengdao

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.