S
S
symblight2017-03-01 12:23:09
JavaScript
symblight, 2017-03-01 12:23:09

How to address requests if they are ignored, the only request is to index.html?

I'm using webpack-dev-middleware , and accessing index.html like this:

app.get('/*',(req,res)=>{
      res.sendFile(path.join(__dirname,'./public/index.html'));
  });

the trouble is that I have other queries:
router.get('/list',(req,res)=>{
    ...
});

router.post('/create',(req,res)=>{
  ...
});

router.delete('/del/:id',(req,res)=>{
    ....
});

export default router;

and then I address
app.use('/*',api);
Because I address index.html, on any request I receive index.html. How can I combine and am I doing it right?
The code
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.js
import 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.js
import 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();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Chebotarev, 2017-03-09
@Egche

With this entry,
you intercept all the paths `/...` and give index along them.
You can put it in index.js (well, remove the interception in server.js - I vanguy that the first code example is called that)

router.get('/',(req,res)=>{
      res.sendFile(path.join(__dirname,'./public/index.html'));
  });

And everything should work.
And that's right, give statics with nGinx.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question