Next.js custom server - javascript

I have a Next.js application and I want to connect it to a Node.js backend.
If i put the file server.js in the same folder of the Next.js application I can run the application through these scripts:
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "set NODE_ENV=production node server.js",
"lint": "next lint"
}
I want to put all the backend files in another folder (server.js too) , but in this case I can't run the application.
What I have to do? Thanks

Related

Run script after vue development server starts

I have a script called modifyFile.js that I would like to run right after the vue development server starts. Currently, I have the dev script as "vite && npm run modifyFile" However, the modifyFile script never runs since vite spins up a server and the server is still running.
How do I go about fixing it? I want to run both actions in one command - npm run dev.
// package.json
{
"name": "test",
"version": "0.0.0",
"scripts": {
"dev": "vite && npm run modifyFile",
"modifyFile": "node modifyFile.js",
"build": "vite build",
"preview": "vite preview --port 4173",
},
}

How to add TS files to launch vue3 application in prod

Current folder structure:
/appname/server.js
/appname/package.json
Current package.json
"scripts":
{
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"start": "node server.js",
},
I'd like to write serve.js using TS. I know how to do in a typical TS project, but i'm not able to do so when webpacks are involved. If I update tsconfig to include serve.ts the js file doesn't end up in the dist folder.
Wanted folder structure:
/appname/server.ts
/appname/package.json
Wanted package.json
"scripts":
{
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"start": "node dist/server.js",
},
To make this even more complicated, the dist folder is needed to drive the application, so I rather not mess with it, putting server.js in a different folder is probably a better approach.
You could use ts-node to run server.ts directly:
Install ts-node as a devDependency:
npm i -D ts-node
Update your NPM script to use ts-node instead of node:
{
"scripts": {
"start": "ts-node server.ts"
}
}
Or you can avoid a devDependency with npx ts-node:
{
"scripts": {
"start": "npx ts-node server.ts"
}
}

React and node app in firebase not working?

I have a React App with Node(Express) as the backend. React is requesting for an end point /api/users and Node is serving with a static response. This is working fine in local.
The same app has been deployed in firebase and the React part is not able to consume the Node /api/user part, api call failing.
Below is the project structure -
The node part is in server.js
Package.json has the proxy part directing to the Node Port
The scripts part in package.json is as given below -
"scripts": {
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"server": "nodemon server.js",
"start": "concurrently \"npm run server\" \"react-scripts start\""
},
Not sure what is missing? Just to reiterate that this is working in my local, but not working after deploying to firebase

Deploy React-(Create-React-App),Express.js and MySQL

How to deploy react (Create-React-App), Express.js and MySQL ?
I tried deploying on cPanel. do I have to change it to Production mode?
how do I access the API on Express.js? do I have to create a sub domain on Express.js?
because so far I only use the library which is concurrently and nodemon. and on React.js I added a proxy like this:
"proxy": "http: // localhost: 5000", where localhost: 5000 has a port from Express.js
Package.json script in server.js
"scripts": {
"client-install": "npm install --prefix client",
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"dev": "concurrently --kill-others \" npm run server\" \"npm run client\""
},
Package.json script in React
"proxy": "http://localhost:5000",
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
My App :
Client > folder react app > Package.json,Src
Node modules > npm from server
Server > Express config,Routes,Controller
.babelrc
server.js
package.json
Do you have terminal access on the cPanel? If yes, then
Start your node server (Using PS or Nodemon)
If your cPanel is using any kind of a server (nginx or Apache webserver for example), then configure the server file to redirect to localhost:5050 on *:80 and *:443
Configure your Express.js to serve on port 5050
And that's it.

NextJs 5.0.0 Heroku Internal Server Error

These are my scripts:
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "NODE_ENV=production node server.js",
"heroku-postbuild": "next build"
},
This is my procfile:
web: npm start -- --port $PORT
These are my config variables:
NPM_CONFIG_PRODUCTION=false
You have to update next to 5.0.1-canary.4 this was actually a nextjs bug.

Categories