[React] 19 - Project: Book Worm: Server nodemon 基本配置与使用

前言


 

Ref: Build Real Web App with React #02 - 20:04 / 34:47

开始介绍后端搭建。

 

 

 

 

 

代码分析


一、搭建HTTP服务器

  • 安装 bebal
yarn add --dev babel-cli babel-preset-env

配置 package.json ----> 自动执行bebal,when code is changed.

Ref: nodemon 基本配置与使用,使 bebal 自动化。

"scripts": {
  "start": "nodemon --exec babel-node -- src/index.js"
},

 

  • Express http 服务器搭建

(1) index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Bookworm API</title>
</head>
<body>

  <h1>This is API, not a website. Learn your routes, mate!</h1>

</body>
</html>

要显示的内容。

 

(2) index.js

import express    from "express";
import path       from "path";
import mongoose   from "mongoose";
import bodyParser from "body-parser";
import dotenv     from "dotenv";
import Promise    from "bluebird";

import auth  from "./routes/auth";
import users from "./routes/users";
import books from "./routes/books";

dotenv.config();
const app = express();
app.use(bodyParser.json());
mongoose.Promise
= Promise; mongoose.connect(process.env.MONGODB_URL, { useMongoClient: true }); app.use("/api/auth", auth); app.use("/api/users", users); app.use("/api/books", books); app.get("/*", (req, res) => { res.sendFile(path.join(__dirname, "index.html")); }); app.listen(8080, () => console.log("Running on localhost:8080"));

 

 

 

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/jesse123/p/9234536.html