jamesmoran
jamesmoran
Explore posts from servers
TTCTheo's Typesafe Cult
Created by Bart ⚡ on 4/7/2023 in #questions
T3 Stack + custom Next.js server not working
@asmodig remove the env import from the next config file
22 replies
TTCTheo's Typesafe Cult
Created by Bart ⚡ on 4/7/2023 in #questions
T3 Stack + custom Next.js server not working
I see, so you have moved the env import outside of you next config. That would fix the problem I am running into right now!
22 replies
TTCTheo's Typesafe Cult
Created by Bart ⚡ on 4/7/2023 in #questions
T3 Stack + custom Next.js server not working
I did not, but I just saw that the build failed. Looks like I have a bit more tinkering to do to get this working…
22 replies
TTCTheo's Typesafe Cult
Created by Bart ⚡ on 4/7/2023 in #questions
T3 Stack + custom Next.js server not working
After trying to add some functionality to my custom server, I realised: - Paths I had set up in my tsconfig.json were not recognised - The .mjs file extension is not supported The paths fix was easy. All I did was: 1. Install the following dependency:
npm install -D tsconfig-paths
npm install -D tsconfig-paths
2. Add the following to my tsconfig.server.json file:
"ts-node": {
"require": ["tsconfig-paths/register"]
},
"ts-node": {
"require": ["tsconfig-paths/register"]
},
Now, for the .mjs file, I really wanted to keep the .mjs file extension, but I just couldn't figure out a way to get it working. In the end, I reluctantly converted
src/env.mjs → src/env.js. If anyone figures out how to get it working, I would love to hear it!
22 replies
TTCTheo's Typesafe Cult
Created by Bart ⚡ on 4/7/2023 in #questions
T3 Stack + custom Next.js server not working
These are the steps I took: 1. Install dependencies:
npm install -D nodemon ts-node
npm install -D nodemon ts-node
2. Update package.json scripts:
"dev": "NODE_ENV=development nodemon",
"build": "next build && tsc --project tsconfig.server.json",
"start": "NODE_ENV=production node dist/server.js",
"dev": "NODE_ENV=development nodemon",
"build": "next build && tsc --project tsconfig.server.json",
"start": "NODE_ENV=production node dist/server.js",
3. Add nodemon.json file:
{
"watch": ["server.ts"],
"exec": "ts-node --project tsconfig.server.json server.ts",
"ext": "js ts"
}
{
"watch": ["server.ts"],
"exec": "ts-node --project tsconfig.server.json server.ts",
"ext": "js ts"
}
4. Update tsconfig.json "checkJs": true to "checkJs": false 5. Add tsconfig.server.json file:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "dist",
"lib": ["es2019"],
"target": "es2019",
"isolatedModules": false,
"noEmit": false
},
"include": ["server.ts"]
}
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "dist",
"lib": ["es2019"],
"target": "es2019",
"isolatedModules": false,
"noEmit": false
},
"include": ["server.ts"]
}
6. Add the server.ts file:
import { createServer } from "http";
import next from "next";
import { parse } from "url";

const port = parseInt(process.env.PORT || "3000", 10);
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();

app
.prepare()
.then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url!, true);
void handle(req, res, parsedUrl);
}).listen(port);

console.log(
`> Server listening at http://localhost:${port} as ${
dev ? "development" : process.env.NODE_ENV
}`
);
})
.catch((err) => {
console.error(err);
process.exit(1);
});
import { createServer } from "http";
import next from "next";
import { parse } from "url";

const port = parseInt(process.env.PORT || "3000", 10);
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();

app
.prepare()
.then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url!, true);
void handle(req, res, parsedUrl);
}).listen(port);

console.log(
`> Server listening at http://localhost:${port} as ${
dev ? "development" : process.env.NODE_ENV
}`
);
})
.catch((err) => {
console.error(err);
process.exit(1);
});
Sources: - https://nextjs.org/docs/advanced-features/custom-server - https://github.com/vercel/next.js/tree/canary/examples/custom-server
22 replies
TTCTheo's Typesafe Cult
Created by Bart ⚡ on 4/7/2023 in #questions
T3 Stack + custom Next.js server not working
I got this working. I’m not at my computer right now, but happy to share the steps I took when I’m back
22 replies
TTCTheo's Typesafe Cult
Created by jamesmoran on 4/17/2023 in #questions
Simplest way to add cron job scheduler (PostgreSQL)
Thank you! I will stick to the custom server in that case
4 replies