Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run well when dev, but i get a 404 after build. #2038

Closed
DarkPage opened this issue May 22, 2017 · 3 comments
Closed

Run well when dev, but i get a 404 after build. #2038

DarkPage opened this issue May 22, 2017 · 3 comments
Labels
please add a complete reproduction The issue lacks information for further investigation

Comments

@DarkPage
Copy link

I fllow the lesson: 7.Fetching Data for Pages, i make a simple api like this:

// server.js
const express = require('express');
const bodyParser = require('body-parser');
const next = require('next');

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
const cors = require('cors');

var user = require('./controller/user.controller.js');  // orm sequelize

app.prepare().then(() => {

    const server = express();

    server.use(bodyParser.json());                              // for parsing application/json
    server.use(bodyParser.urlencoded({ extended: true }));      // for parsing application/x-www-form-urlencoded
    server.use(cors());

    server.get('/api/v1/user', (req, res) => {
        user.findAll(req, res);
    });

    server.get('*', (req, res) => {
        return handle(req, res)
    });

    server.listen(3000, (err) => {
        if (err) throw err
        console.log('> Ready on http://localhost:3000')
    });
}).catch((ex) => {
    console.error(ex.stack)
    process.exit(1)
});
// pages/index.js
import Header from '../components/Header';
import axios from 'axios';
import React, { Component } from 'react';
import ReactDOM from 'react-dom';

// class style
export default class Index extends Component {

	static async getInitialProps() {
		try {
			const res = await axios.get('http://localhost:3000/api/v1/user');
			const { data } = res;
			console.log(`User data fetched.`)
			return {
				users: data || []
			}
		} catch (error) {
			console.log(error)
			return {
				users: []
			}
		}
	}

	render() {
		let html = this.props.users.map(user=>{
			return (
				<div className="user">
					<h5>{user.id}</h5>
					<p>{user.firstName}</p>
					<div>{user.lastName}</div>
				</div>
			)
		});
		return (
			<div>
				<Header />
				<p>Hello Next.js</p>
				<div>{html}</div>
				<style jsx global>{`
				p{
					font-size: 14px;
				}
				.user{
					border-bottom: 1px solid black;
					background: url('./static/kitty.png') no-repeat;
				}
				`}</style>
			</div>
		)
	}
}
// deploy
$npm run build
$PORT=3000 npm run start // script next start -p $PORT

// http://localhost:3000/ refresh i got 404
{ Error: Request failed with status code 404
    at createError (/Users/wangxin/Desktop/next-k12/node_modules/axios/lib/core/createError.js:15:15)
    at settle (/Users/wangxin/Desktop/next-k12/node_modules/axios/lib/core/settle.js:18:12)
    at IncomingMessage.handleStreamEnd (/Users/wangxin/Desktop/next-k12/node_modules/axios/lib/adapters/http.js:191:11)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:188:7)
    at endReadableNT (_stream_readable.js:975:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)
  config:
   { adapter: [Function: httpAdapter],
     transformRequest: { '0': [Function: transformRequest] },
     transformResponse: { '0': [Function: transformResponse] },
     timeout: 0,
     xsrfCookieName: 'XSRF-TOKEN',
     xsrfHeaderName: 'X-XSRF-TOKEN',
     maxContentLength: -1,
     validateStatus: [Function: validateStatus],
     headers:
...

why error ?

@arunoda
Copy link
Contributor

arunoda commented May 23, 2017

It's hard to answer like this.
Feel send us a sample repo.

@arunoda arunoda closed this as completed May 23, 2017
@arunoda arunoda added the please add a complete reproduction The issue lacks information for further investigation label May 23, 2017
@arunoda
Copy link
Contributor

arunoda commented May 23, 2017

Please re-open once you have it.

@miladmeidanshahi
Copy link

same issue :\

@lock lock bot locked as resolved and limited conversation to collaborators May 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
please add a complete reproduction The issue lacks information for further investigation
Projects
None yet
Development

No branches or pull requests

3 participants