How to serve NodeJS form app using zeit now server - node.js

I am running a simple nodejs form processing app on zeit now server and deploying using now CLI scripts.
This Node app although working well on the localhost within the said port number (like this: http://localhost:8081/myaction), however the same function is not workinig for me on the zeit server wherein I replace the localhost with the server URL given by NOW build script.
Here is my now.json and I am also providing access to the git repository which has the node server app thaat processes the form, sends an E-mail and serves a Thank You page in the last step.
Node Server App repo: https://github.com/jnsrikanth/node-form-server2
now.json file contents:
{
"version": 2,
"builds": [{ "src": "index.js", "use": "#now/node-server" }],
"routes": [
{
"src": "/public/thank-you/dist/",
"dest": "/index.html",
"methods": ["GET"]
},
{
"src": "/",
"dest": "/index.js",
"methods": ["POST"]
}
]
}
The package.json and the remaining code can be found in the repository. It would be great if I can find some pointers here.
Also please note that in my form I am calling action="https://zeit-url:8081/myaction" method="POST" where 8081 is the port on which the express app is listening for processing the POST method with form data.

In your now.json replace / with /myaction
...
{
"src": "/myaction",
"dest": "/index.js",
"methods": ["POST"]
}
...
And just call https://zeit-url/myaction (without port number)
I hope it will help you

I would suggest you to convert that now.json into a vercel.json and, based on the github repo you provided, update it so looks like:
"version": 2,
"builds": [
{
"src": "index.js",
"use": "#vercel/node"
}
],
"routes": [
{
"src": "/public/thank-you/dist/",
"dest": "/public/index.html",
"methods": ["GET"]
},
{
"src": "/",
"dest": "/index.js",
"methods": ["POST"]
}
]
You need to verify this server runs without problems on local machine.
Since you are using the builds vercel.json option, you need to ensure all the dependencies, as node_modules etc are uploaded to vercel, either by deploying file from the vercel-cli, or using some pipeline or CI action.

Related

Error while deploying Node js application to Vercel

Trying to deploy Node server to Vercel but getting the following error:
The structure:
Besides the versel.json file is present:
{
"version": 2,
"builds": [
{
"src": "./server.js",
"use": "#vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/"
}
]
}
So what could be the reason for the error?

Cannot deploy new API routes using Vercel (Zeit Now)

I've been developing an Node.js/Express.js API and running it on localhost. I set up the API to be hosted on Zeit Now (currently Vercel).
When I first started the API, I deployed 2 routes to Zeit Now using the "Now" command. Those routes worked fine when I accessed them. Now about half a year later, I tried to deploy a bunch of additional routes view the "Now" command, but when I go to access the new routes on the deployed version, they return a 404 status.
The new routes are in now.json.
This is the format of this file:
{
"version": 2,
"builds": [{ "src": "index.js", "use": "#now/node-server" }],
"routes": [
{
"src": "/login",
"dest": "/index.js",
"methods": ["POST"]
},
{
"src": "/exampleget",
"dest": "/index.js",
"methods": ["GET"]
},
{
"src": "/examplepost",
"dest": "/index.js",
"methods": ["POST"]
}
]
}
The route that is not working is /login. I've also tried with other routes not shown in this file. They didn't work either.
Can I no longer deploy routes this way after the transition from Zeit to Vercel?

Problem to deploy a node / express API using now.sh

I'm trying to deploy a node / express API that has several endpoints for use at now.sh.
My question is how to correctly configure the file now.json.
leave the image where you can see the complete structure of the project, the main path would be the ./src/ where are all the folders and files.
The main entrypoint is http://localhost:3000/api/v1/ for localhost
I delete the file now.json and from the terminal I paste the now command, and then I generated the deployment in the following path
https://ryuanime.chrismichael.now.sh
But when I try to go to https://ryuanime.chrismichael.now.sh/api/v1 it shows me 404: NOT_FOUND
Image
source code
now.json
{
"version": 2,
"name": "ryuanime",
"builds": [
{
"src": "src/index.ts",
"use": "#now/node-server"
}
],
"routes": [{"src": "/(.*)", "dest": "/src/index.ts"}]
}
For some reason it worked by referring to
"src": "./src/index.ts"
and not
"src": "src / index.ts",
{
"name": "ryuanime-api",
"version": 2,
"builds": [
{
"src": "./src/index.ts",
"use": "#now/node-server"
}
],
"routes": [
{ "src": "/(.*)", "dest": "/src/index.ts" }
]
}

How to setup now index.html?

I am trying to deply my node.js app to NOW,
But when i deploy all i see is the root folder with all the files...
I am supposed to have an index.html file, but i dont know where this happens or where i should set this up?
In webpack? in start.js?
Any ideas?
In my project, i have start.js and app.js
So I guess, I should use start.js on the now.json config file
{
"version": 2,
"builds": [
{ "src": "start.js", "use": "#now/node" },
{ "src": "index.html", "use": "#now/static" }
]
}
But I get an error and logs like:
'The lambda function size (8.98mb) exceeds the configured limit (5mb). You may increase this by supplying maxLambdaSize to the build config'`
As stated in the error, You have to add maxLambdaSize to your build config.
{
"name": "Your Project Name",
"alias": ["yourAliasGoesHere"],
"version": 2,
"builds": [
{
"src": "index.js", //put your own stuff here
"use": "#now/node-server",
"config": { "maxLambdaSize": "10mb" } //right here
}
]
}
Increase or decrease the maxLambdaSize as needed.

web app banner install

I'm using this page: https://developers.google.com/web/fundamentals/engage-and-retain/app-install-banners/ to create a web app banner install.
I have the sw.js as service worker in public/admin/
I have registered with success the service worker
and i have the manifest.json in public/admin too like this
{
"name": "Web App Install",
"short_name": "Admin",
"icons": [
{
"src": "/admin/mstile-70x70.png",
"sizes": "36x36",
"type": "image/png"
},
{
"src": "/admin/mstile-70x70.png",
"sizes": "48x48",
"type": "image/png"
},
{
"src": "/admin/mstile-70x70.png",
"sizes": "72x72",
"type": "image/png"
},
{
"src": "/admin/mstile-70x70.png",
"sizes": "96x96",
"type": "image/png"
},
{
"src": "/admin/mstile-70x70.png",
"sizes": "144x144",
"type": "image/png"
},
{
"src": "/admin/mstile-70x70.png",
"sizes": "192x192",
"type": "image/png"
}
],
"start_url": ".",
"display": "standalone",
"related_applications": [{
"platform": "web"
}],
"related_applications": [],
"prefer_related_applications": false
}
in dev tools
the link of service worker is: https://01f1c2fb.ngrok/admin/pt
the link of manifest.json is https://01f1c2fb.ngrok/admin/manifest.json
but i'm getting an error on trying install the web app
the error is
"Site cannot be installed: no matching service worker detected. You may need to reload the page, or check that the service worker for the current page also controls the start URL from the manifest"
Can someone help me please.
i add a folder /pt in admin as admin/pt
and added the manifest.json.
i got some errors of image when i clicked to "add to homescreen" but i fixed it
but now clicking on "add to homescreen" he doesnt fire anything...
its all alright for now ?
i tried i mobile phone but not installing ...
can someone help
thanks
Carlos Vieira
i already have made some changes and i get
"beforeinstallprompt Event fired" in console using
window.addEventListener('beforeinstallprompt', function(e) {
console.log('beforeinstallprompt Event fired');
});
but no app in mobile is installed
Thanks in advance.
Carlos Vieira
I've had a similar problem.
You have your sw.js file in public/admin/, this is wrong. Move it to root directory / and serviceWorker should init fine.

Resources