Skip to content

Commit

Permalink
fix(nextjs): update custom server setup so hostname and port are alwa…
Browse files Browse the repository at this point in the history
…ys provided
  • Loading branch information
jaysoo authored and Jack Hsu committed May 2, 2022
1 parent cb8bf52 commit 53e12da
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 64 deletions.
3 changes: 2 additions & 1 deletion docs/generated/packages/next.json
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,8 @@
},
"hostname": {
"type": "string",
"description": "Hostname on which the application is served."
"description": "Hostname on which the application is served.",
"default": "localhost"
},
"proxyConfig": {
"type": "string",
Expand Down
3 changes: 2 additions & 1 deletion packages/next/src/executors/server/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
},
"hostname": {
"type": "string",
"description": "Hostname on which the application is served."
"description": "Hostname on which the application is served.",
"default": "localhost"
},
"proxyConfig": {
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,67 +57,5 @@ export function createNextServerFiles(host: Tree, options: NormalizedSchema) {
}
`
);

const { appsDir } = getWorkspaceLayout(host);

host.write(
joinPathFragments(directory, 'server.js'),
`
// @ts-check
'use strict';
/**
* Production Nextjs custom server
*
* Usage: run this script with node
* Adjust dir option for your serve/deploy config
*
* node server.js
*/
/**
* @typedef {import(${
isOldNext
? 'next/dist/next-server/server/next-server'
: 'next/dist/server/next-server'
}).default} Server
*/
const NextServer = require(${
isOldNext
? 'next/dist/next-server/server/next-server'
: 'next/dist/server/next-server'
}).default;
const express = require('express');
const nextApp = new NextServer({
dir: './dist/${appsDir}/<%= name %>',
staticMarkup: false,
quiet: false,
conf: {
distDir: '.'
}
});
const serve = async () => {
const handle = nextApp.getRequestHandler();
const expressApp = express();
await nextApp.prepare();
return new Promise((resolve, reject) => {
expressApp.all('*', (req, res) => {
return handle(req, res);
});
const server = expressApp.listen(4200, err => {
err ? reject(err) : resolve(server);
});
});
}
serve().then(server => console.log('Server is running on port 4200'));
`
);
}
}

0 comments on commit 53e12da

Please sign in to comment.