Issue 1:
I have searched the issues of this repository and believe that this is not a duplicate.

I'am currently switching my existing app to next.js , everything seems to work correctly except my api end point (running on port:4000), which i can't proxy from my next.js app. I followed the examples in the repo but i can't make it work, In production i use nginx as reverse proxy with no problem , but i'am in dev mode . For setting up Apollo with Redux i followed this example :with-apollo-and-redux and for proxy i used this example with-custom-reverse-proxy

Expected Behavior

Get a response from my api end point

code

in initApollo.js

import { ApolloClient, createNetworkInterface } from "react-apollo";
import fetch from "isomorphic-fetch";

let apolloClient = null;

if (!process.browser) {
global.fetch = fetch;
}

function create() {
return new ApolloClient({
ssrMode: !process.browser,
networkInterface: createNetworkInterface({
uri: "/api"
})
});
}

export default function initApollo() {
if (!process.browser) {
return create();
}

if (!apolloClient) {
apolloClient = create();
}

return apolloClient;
}

and in server.js

const express = require("express");
const next = require("next");

const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
// const httpProxy = require('http-proxy');
// const apiProxy = httpProxy.createProxyServer();
// const apiServer = 'http://localhost:4000',

const devProxy = {
"/api": {
target: "http://localhost:4000/api/",
changeOrigin: true
}
};

app
.prepare()
.then(() => {
const server = express();

if (dev && devProxy) {
const proxyMiddleware = require("http-proxy-middleware");
Object.keys(devProxy).forEach(function(context) {
server.use(proxyMiddleware(context, devProxy[context]));
});
}

server.all("*", (req, res) => 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);
});

Current Behavior

POST http://localhost:3000/api 501 (Not Implemented)

Your Environment

Tech
Version

next
3

node
8

OS
osx

browser
chrome

etc

Issue 2:
Bug report

Describe the bug

Loving v9! Just ran into an issue where I can’t use an existing database connection with the new API routes.

To Reproduce

mongooseConnect.js:

const mongoose = require("mongoose").set("debug", true);
const connection = {};

module.exports = async () => {
if (connection.isConnected) {
// Using existing database connection
return;
}

// Using new database connection
const db = await mongoose.connect("mongodb://localhost:27017/my-app",
{ useCreateIndex: true, useFindAndModify: false, useNewUrlParser: true }
);

connection.isConnected = db.connections[0].readyState;
};

Require the above function into an API endpoint that uses the new pages/api/ feature and requires

Run next dev

Ping the API endpoint and see the correct response

Edit mongooseConnect.js and save

Ping the API endpoint again

Expected behavior

The response will be the same. Instead, I get the following error:

MongooseError: You can not `mongoose.connect()` multiple times while connected.

This is because db returns to {} with the HMR.

System information

OS: macOS

Version of Next.js: 9.0.0

Additional context

Is there a different way we should be handling serverless database connections or is this a bug?