我需要使用Node js,Express,MongoDB和Angular Js做一个“做APP”来进行企业测试。 我从不使用Stack MEAN,但目的是发现它! 该企业已将包含路线和与mongodb数据库的连接的基础项目发送给我。
因此,我只需要为待办事项应用创建功能(添加新任务,更新任务,删除,按ID搜索以及显示所有任务)。
但是在理解路由文件以及如何将我的功能与前端的Angular应用程序连接之后,我有些麻烦。是的,我对此项目知之甚少。
这是Routes文件:
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
}
这是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
}
答案 0 :(得分:0)
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.