简体   繁体   English

我在 MEAN 堆栈中的 To Do App 开发需要帮助

[英]I need help for my To Do App development in MEAN stack

I need to make a "to do APP" with Node js, Express, MongoDB & Angular Js for an enterprise's test.我需要用 Node js、Express、MongoDB & Angular Js 做一个“做 APP”,用于企业测试。 I've never work with the Stack MEAN but the purpose is to discover it.我从未使用过 Stack MEAN,但目的是发现它。 The enterprise has sent me the base project with the routes and the connection to mongodb database.企业已向我发送了带有路线的基础项目以及与 mongodb 数据库的连接。

Therefore, I have just to create function for my todo app (add new task, update task, delete, search by id, and show all tasks).因此,我只需为我的待办事项应用程序创建 function(添加新任务、更新任务、删除、按 id 搜索并显示所有任务)。

But I have some troubles to understand the routes files and after how to connect my functions with a Angular App for the front-end.但是在理解路由文件以及如何将我的功能与前端的 Angular 应用程序连接之后,我遇到了一些麻烦。 Yes I know little about this project.是的,我对这个项目知之甚少。

This is the Routes file:这是路由文件:

const express = require('express')
const router = express.Router()

module.exports = (mongoose) => {

  const Todo = mongoose.model('Todo')

  /**
   * Get all todos, it should support basic querying with Query Parameters
   *
   * e.g.: GET /todo/?task=myTask&done=true
   */
  async function getAllTodos (req, res, next) {
    const result = await Todo.find()
    return res.status(200).json(result)
  }

  /**
   * Get single todo
   */
  async function getTodoById (req, res, next) {
    return res.status(200).json({'hello': 'world'})
  }

  /**
   * Update single todo
   */
  async function updateTodoById (req, res, next) {
    return res.status(200).json({'hello': 'world'})
  }

  /**
   * Create new todo
   */
  async function createNewTodo (req, res, next) {
    return res.status(200).json({'hello': 'world'})
  }

  /**
   * Delete single todo
   */
  async function deleteTodoById (req, res, next) {
    return res.status(200).json({'hello': 'world'})
  }

  router.get('/?', getAllTodos)
  router.get('/:id', getTodoById)
  router.put('/:id', updateTodoById)
  router.post('/?', createNewTodo)
  router.delete('/:id', deleteTodoById)
  return router
} 

And here the app.js file:这里是 app.js 文件:

const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const todoRoutes = require('./routes/todo/todo.route')
const getMongoose = require('./database')

module.exports = async () => {
  const app = express()
  // Get mongoose instance
  const mongoose = await getMongoose()

  // Add minimal middlewares for basic queries
  app.use(cors())
  app.use(bodyParser.json())

  // Add todo routes
  app.use('/todo', todoRoutes(mongoose))


  // Add fallback for uncaught routes
  app.use((req, res, next) => {
    return res.status(501).json('Not Implemented')
  })
  return app
}
Refer to this link step by step:
https://www.codementor.io/shanewignall/making-a-restful-backend-with-node-js-knf7nbsii

step:1
create one folder
Ex:backend-demo

step:2
open cmd
run command: npm init
Then you can see as below
package name: (backend-demo) then press enter,enter..
last you can view:
{
  "name": "backend-demo",
  "version": "1.0.0",
  "description": "backend-demo",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}
step:3
run cmd:npm i
step:4
create server.js file

server.js file:
const http = require('http');
const app = require('./config/app'); // The express app we just created

const port = 3000;
app.set('port', port);

const server = http.createServer(app);
server.listen(port, () => {
  console.log(`Express server listening on port ${port}`);
});
step:5
create app.js file inside config folder
app.js file:
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
app.use(bodyParser.urlencoded({ limit: '2gb', extended: true }));
app.use(bodyParser.json({ limit: '2gb', extended: true }));
app.use(cors());
// Parse incoming requests data (https://github.com/expressjs/body-parser)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Require our routes into the application.
app.use(require('../routes'));
app.all('/*', (req, res) => {
  return res.status(404).json({
    errors: { message: req.t('INVALID_REQUEST') },
    status: 1
  });
});
module.exports = app;
step:6
You have to create route and model files.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM