使用Node.js构建实时Markdown编辑器

介绍 (Intro)

Markdown is a popular text format written in an easy-to-read way and is convertible to HTML. It is a markup format that has been popularized by sites such as Github and Stack Overflow. Today we will be building an app that let's us view the raw markdown on the left side and the converted markdown (to HTML) on the right side. We will also allow multiple people to work on the same markdown document at the same time via a shareable URL and all changes will be saved.

Markdown是一种流行的文本格式,易于阅读,并且可以转换为HTML。 这是一种标记格式,已被Github和Stack Overflow等网站所普及。 今天,我们将构建一个应用程序,让我们在左侧查看原始markdown,在右侧查看转换的markdown(至HTML)。 我们还将允许多个人通过可共享的网址同时处理同一个Markdown文档,并且所有更改都将被保存。

设置节点 (Setup Node)

Let's get started on our real time markdown viewer app. We will be creating our backend in Node for this application. Create a project directory and then from the command line run the following:

让我们开始使用我们的实时降价查看器应用程序。 我们将在Node中为此应用程序创建后端。 创建一个项目目录,然后从命令行运行以下命令:

npm init

This will prompt us with several questions. You can fill in the prompts accordingly. This will create a package.json file. Here is my sample package.json file.

这将提示我们几个问题。 您可以相应地填写提示。 这将创建一个package.json文件。 这是我的示例package.json文件。

{
      "name": "RealtimeMarkdownViewer",
      "description": "Realtime Markdown Viewer",
      "main": "server.js",
      "version": "1.0.0",
      "repository": {
        "type": "git",
        "url": "[email protected]:sifxtreme/realtime-markdown.git"
      },
      "keywords": [
        "markdown",
        "realtime",
        "sharejs"
      ],
      "author": "Asif Ahmed",
      "dependencies": {
        "express": "^4.12.4",
        "ejs": "^2.3.1",
        "redis": "^0.10.3",
        "share": "0.6.3"
      },
      "engines": {
        "node": "0.10.x",
        "npm": "1.3.x"
      }
    }

Now let's create a server.js file in our root directory. This will be the main server file. We will be using Express as our web application framework. Using Express makes building a server simpler. With Express we will be using EJS for our view templates. To install Express and EJS run the following commands:

现在,让我们在根目录中创建一个server.js文件。 这将是主服务器文件。 我们将使用Express作为我们的Web应用程序框架。 使用Express可以简化服务器的构建。 使用Express,我们将使用EJS作为视图模板。 要安装Express和EJS,请运行以下命令:

node install --save express
node install --save ejs

Also create a views folder and a public folder in the root directory.. The views folder is where we will be putting our EJS templates and the public folder is where will be serving our assets (stylesheets, javascript files, images). Now, we are ready to add some code to our server.js file.

还可以创建一个views文件夹和一个public的根目录文件夹。该views文件夹是我们将会把我们的EJS模板和public文件夹是将服务于我们的资产(样式表,JavaScript文件,图像)。 现在,我们准备将一些代码添加到我们的server.js文件中。

// server.js

var express = require('express');
var app = express();

// set the view engine to ejs
app.set('view engine', 'ejs');

// public folder to store assets
app.use(express.static(__dirname + '/public'));

// routes for app
app.get('/', function(req, res) {
  res.render('pad');
});

// listen on port 8000 (for localhost) or the port defined for heroku
var port = process.env.PORT || 8000;
app.listen(port);

Here we require the Express module, set the rendering engine to EJS, and we have a route for our home page. We also set the public directory to be a static directory. Lastly we set the server to listen on port 8000. From our home route, we will be rendering a file called pad.ejs from the view directory. This is a sample views/pad.ejs file.

在这里,我们需要Express模块​​,将渲染引擎设置为EJS,并且我们的主页具有路由。 我们还将public目录设置为静态目录。 最后,我们将服务器设置为侦听端口8000。从本地路由开始,我们将从view目录中渲染一个名为pad.ejs的文件。 这是一个示例views/pad.ejs文件。

<!-- views/pad.ejs -->

<!DOCTYPE html>
<html>
<head> 
    <title>Realtime Markdown Viewer</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
</head>

<body class="container-fluid">
Hello World!
</body>
</html>

For styling, we added Bootstrap. Let's start up our node server (node server.js) and go http://localhost:8000 in our web browser. You should see something like this:

对于样式,我们添加了Bootstrap 。 让我们启动节点服务器( node server.js ),并在Web浏览器中进入http://localhost:8000 。 您应该会看到以下内容:

image1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
打造Node.js全栈开发工程师,视频教程内容包括以下内容:一、Node基础知识:学习Node中的核心模块、核心变量,如何操作文件以及安装管理第三方的模块,为大家后面进入服务器端的学习打下扎实的基础;二、Node服务器开发:这部分内容主要是讲解了在工作中最常用到的web服务器知识,包括如何使用node建立网站服务器供客户端访问以及如何把node作为客户端向服务器请求数据;三、最经典的Node快速开发框架Express:这部分内容主要是讲解了Express的原理和用法,可以利用express框架快速高效地开发主流的web应用和实时应用,可以胜任企业级的Node开发要求,这部分知识也是面试中频繁出现的技术点;四、最主流的非关系型数据库Mongodb:这部分内容主要是讲解了mongdb在实际项目中的使用。并会讲解一个基于bootstrap+express+mongodb实现一个包括用户管理、文章管理、查看留言、分页查询、 搜索、文件上传、pv留言统计等功能完整的博客系统。使用了express的路由、ejs模板和serve-favicon、 morgan、cookie-parser、body-parser、express-session、connect-mongo、connect-flash、uuid、 async等内置各种中间件以及其它路径保护等自定义中间件,并扩展了富文本编辑器markdown和heroku云布署等功能。五、主流的实时通信解决方案websocket和socket.io:这部分我们使用socket.io搭建一个功能完善的聊天室。包括匿名聊天、具名聊天、划分不同的房间、私聊等功能。并且会将数据保存到数据库中进行持久化;六、实用的网络资源抓取工具-爬虫:本项目基于bower+mongodb+bootstrap的爬虫系统。学习并实战了bower的前台框架依赖管理以及gulp的代码编译, 单元测试, 代码规范校验等自动化构建过程

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值