import api from './server/routers/index.js';
import webpack from 'webpack';
import webpackDev from 'webpack-dev-middleware';
import webpackHot from 'webpack-hot-middleware';
app.use(bodyParser.json());
app.use(morgan('dev'));
app.use(webpackDev(compiler,{
stats:{
colors: true
}
}))
app.use(webpackHot(compiler));
app.use('/*',api);
app.get('/*',(req,res)=>{
res.sendFile(path.join(__dirname,'./public/index.html'));
});
index.jsimport express from 'express';
import * as api from './api.js';
const router = express.Router();
router.get('/list',(req,res)=>{
api.listNote()
.then(data=>res.status(200).send(data))
.catch(err=>res.status(500).send());
});
router.post('/create',(req,res)=>{
api.createNote(req.body)
.then(data=>res.status(200).send(data))
.catch(err=>res.status(500).send());
});
router.delete('/del/:id',(req,res)=>{
api.deleteNote(req.params.id)
.then(data=>res.status(200).send(data))
.catch(err=>res.status(500).send());
});
export default router;
api.jsimport mongoose from 'mongoose';
import '../models/note.js';
const Note = mongoose.model('note');
export function listNote(){
return Note.find();
}
export function createNote(data){
const note = new Note({
title:data.title,
color:data.color,
text:data.text,
createAt:new Date()
});
return note.save();
}
export function deleteNote(id){
return Note.find({_id:id}).remove().exec();
}