简体   繁体   中英

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. I've never work with the Stack MEAN but the purpose is to discover it. The enterprise has sent me the base project with the routes and the connection to mongodb database.

Therefore, I have just to create function for my todo app (add new task, update task, delete, search by id, and show all tasks).

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. 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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