H
H
hollanditkzn2018-01-29 10:34:19
Node.js
hollanditkzn, 2018-01-29 10:34:19

Why does my request display an html page?

I make an axios request, but I get not a response, but html markup, I don’t understand how to fix this error?
Here is my
Home.js implementation

import React, {Component} from 'react';
import { connect } from 'react-redux';
import axios from 'axios';

class Home extends Component{
    constructor(props){
        super(props);
    }
    componentWillMount(){
        axios.get('/api/test-thema').then(response => console.log(response.data));
    }
    ...
}
const mapStateToProps = state => ({
    user: state.login.user
});

export default connect(mapStateToProps)(Home);

in router.js. I do on koa
router.get('/api/test-thema', async ctx => {
        ctx.body = await Test.allThemes(1);
    })

And in the Test model
let Test = {
    allThemes: function(id){
        return requestTest('SELECT name FROM thema id_possition = ?', id);
    },
    ...
};

function requestTest(sql, req) {
    return new Promise((resolve, reject) => {
        db.query(
            sql,
            [req],
            (err, rows) => {
                if(err) reject(err);
                resolve(rows);
            });
    });
}

module.exports = Test;

It only displays the response in the browser console like this
<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta http-equiv="x-ua-compatible" content="ie=edge"><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.4/css/bootstrap.min.css" integrity="2hfp1SzUoho7/TsGGGDaFdsuuDL0LX2hnUp6VkX3CUQ2K4K+xjboZdsXyp4oUHZj" crossorigin="anonymous"><link rel="stylesheet" type="text/css" href="/css/index.css"><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js" integrity="sha384-THPy051/pYDQGanwU6poAc/hOdQxjnOEXzbT+OuUAFqNqFjL+4IGLBgCJC3ZOShY" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.2.0/js/tether.min.js" integrity="sha384-Plbmg8JY28KFelvJVai01l8WyZzrYWG825m+cZ0eDDS1f7d/js6ikvy1+X+guPIB" crossorigin="anonymous"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.4/js/bootstrap.min.js" integrity="VjEeINv9OSwtWFLAtmc4JCtEJXXBub00gtSnszmspDLCtC0I4z4nqz7rEFbIZLLU" crossorigin="anonymous"></script></head><body><div id="content"></div><script src="/client/bundle.js"></script></body></html>

But in the console node does not display anything for some reason, I don’t know why

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2018-01-29
@hollanditkzn

Because the first route always works for you:

.get('*', async ctx => {
  await ctx.render('index');
})

Swap:
.get('/api/logout', async ctx => {
  console.log('router');
  ctx.logout();
  console.log(ctx.state.user);
})
.get('*', async ctx => {
  await ctx.render('index');
})

R
RidgeA, 2018-01-29
@RidgeA

I'll try to guess - Koa and React's dev-server are running on different ports

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question