I want to upload an image using node(11.10.1),pm2(3.4.0),through Ubuntu 16.04. Like many others who have tried, I can receive an image from starting my server through node process but once I use PM2 to upload an image, PM2 restarts the server and there is no image uploaded.
I made a ecosystem and set different types of settings around like , watch, ignore, autoreset, cwd. I also tried the cli line of code where it is "sudo pm2 start server.js --ignore-watch "/public/images". I am not sure how I can ignore Pm2 the right way. I also made sure that i do have a file path at Myapp/public/images/
I have tried the following:
Multer upload files with PM2
Expressjs pm2 ignore watch public/images folder
https://pm2.io/doc/en/runtime/guide/ecosystem-file/
http://pm2.keymetrics.io/docs/usage/watch-and-restart/
my file structure:
>MyApp
>ecosystem.config.js
>server.js
>public
>images
>routes
this is my ecosystem
'apps' : [{
'name': 'pm2_checking_server',
'script': 'server.js',
'cwd': '/var/www/MyApp/'//........also tried it without cwd
'watch': ['server.js'], //...........I also did true/false
'ignore_watch': ['public/images/'],//...did variations like ./public/images, public/images... etc
'watch_options': {
'followSymlinks': false
}
}],
this is my multer destination in MyApp/routes/
destination: function (req, files, cb) {
cb(null, path.join(__dirname , '../public/images'))
}
I expect Pm2 to ignore the watching file upload but the response back I get from uploading an Image is "errno": -13,
"code": "EACCES",
"syscall": "open",
"path": "/var/www/MyApp/public/images/7ylxlncRKJ
thank you for your help!
As far as I see the difference with official examples is you are using single quote ' and they have used double quote " after all this is a json file. Also you have missing a , at line:
"cwd": "/var/www/MyApp/",
Delete trailing / at: public/images/
So this is your process.json file which should be at root of your project:
{
"apps": [{
"name": "pm2_checking_server",
"script": "server.js",
"cwd": "/var/www/MyApp/",
"watch": ["server.js"],
"ignore_watch": ["public/images"],
"watch_options": {
"followSymlinks": false
}
}]
}
Related
I am trying to move my application's API to Vercel. It is written in Typescript and uses Express.
The index.ts is located in <root>/src. The npm run build compiles it into <root>/dist directory. The file contains the following:
const app = express();
app.use((req: Request, res: Response, next: NextFunction) => {
//blah, blah, there is a lot going on here
})
app.use('/', common);
//... other app.use(s)
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server running on ${port}, http://localhost:${port}`));
module.exports = app;
I've got the following in the vercel.json file which is located in the root directory where the package.json also is:
{
"version": 2,
"installCommand": "npm install",
"buildCommand": "npm run build",
"outputDirectory": "dist",
"builds": [
{
"src": "dist/index.js",
"use": "#vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "dist/index.js"
}
]
}
When locally I run npm run build, then vercel dev --listen 5000 I get Ready! Available at http://localhost:5000 and can go to http://localhost:5000/ping and get a response.
Now I commit the files to git, the deployment runs, but judging by the logs the npm install and npm run build commands are not running. No functions are created my /ping endpoint returns "Page not found".
Here is the deployment log:
This is what Build & Development Settings look like (the Root Directory is left blank):
I followed several recommendations I found online and according to them everything should work. I probably miss some setting somewhere. What is it?
If more information is needed, please let me know, I'll update my question.
Thank you.
--- UPDATE ---
I have set the Root Directory to src and checked the "Include source files outside of the Root Directory in the Build Step" checkbox. Now the npm install and npm run build are executing. As you can see some static files are deployed, but there are still no serverless functions and my /ping route returns 404 and "home" page, i.e. / route returns the content of the index.js file. In addition the local is not working either anymore, also returning 404 now.
Without that checkbox I was getting
Warning: The vercel.json file should exist inside the provided root directory
and still no install or build running.
Also worth noting that I had to change my tsconfig.json to have "outDir": "src/dist" instead of "outDir": "dist", otherwise I was getting
Error: No Output Directory named "dist" found after the Build completed. You can configure the Output Directory in your Project Settings.
Removed the Root directory and back to square one, no npm commands running but local is working with / route returning Cannot GET / and /ping returning correct response.
For everyone out there who's looking for an answer, maybe this will help you.
In my case, what I needed is to create a folder, called api in my src folder, i.e. the folder that is specified as Root Directory in Build & Development Settings in Vercel. Within this directory, each serverless function needs a file named the same as the path of the route. For example, the file named "my-route.js" will be accessible via https://my-app-name.vercel.com/api/my-route.
All this file needs is an import of index.js file and module.exports. For example:
import app from '../index';
module.exports = app;
The index.js should also live the Root and contain your express setup.
If you want to have dynamic path parameters, the files' names in the api directory should be wrapped in square brakets, like [my-param.js]. You can also have sub-directories in the api foler.
Here are a few links that helped me figure this out:
https://dev.to/andrewbaisden/how-to-deploy-a-node-express-app-to-vercel-2aa
https://medium.com/geekculture/deploy-express-project-with-multiple-routes-to-vercel-as-multiple-serverless-functions-567c6ea9eb36
https://ryanccn.dev/posts/vercel-framework/#path-segments
No changes were needed in my existing Express setup and routes files.
Hope this will help someone. Took me quite a while to figure it all out :)
I have an electron app which uses a database opened with the following code:
const fs = require("fs")
const sqlite = require("aa-sqlite")
await sqlite.open('cregr_db.db');
My package.json contains :
"build": {
"appId": "crergr",
"linux": {
"target": [
"AppImage"
],
"icon": "icon512.png"
},
"win": {
"target": "NSIS",
"icon": "icon256.ico"
},
"extraFiles": [
"cregr_db.db"
]
},
the cregr_db.db is in the same folder as my *js, index.html and style.css.
Everything runs fine when launched from the root directory of my app.
If I run the app image from elsewhere, I have an exception when I query the database and, in fact, the database file is not displayed in the file hierarchy.
I can not post an image here but you can find one at http://alainbe.free.fr/files.png
What I am doing wrong ?
Thanks for your help.
In fact, the db file was probably added but in an ASAR archive so can't be read by the code posted above.
The solution I chose was to distribute the app without building an ASAR archive and in this case, everything works exactly like you are testing your app (tested on Linux and Windows).
There is a strong warning about building an app without an ASAR. I chose not to pay attention because any way there are ways to unpack the ASAR and see the code and besides, my app is GPL'ed.
I am running PM2 on windows 10 and have the following as my ecosystem.config.json file use to start the process.
module.exports = {
apps : [{
name : "myApp",
script : "./server.js",
watch : true,
watch_delay: 5000,
ignore_watch: ['node_modules', 'tracking'],
env: {
"NODE_ENV": "production",
}
}]
}
Whenever I update something in the tracking directory is causes a restart. If I move the file location to node_modules and remove tracking from the ignore_watch then it works. I have tried several other paths but anything other than node_modules does NOT seem to work with PM2.
Is the formatting incorrect or am I just missing something or is there a known issue in windows?
PM2 watch and unwatch folder and transpile with babel-node
I was looking for a simple solution for the development server!
Problem: When uploading files to the development server 'express-fileupload' PM2 detects a change in the folders and restarts the process.
I clarify this operation is fine because we had PM2 configured to monitor the entire project!
"ecosystem.config.js" is the configuration; it is simple. You can expand it with the official documentation of PM2. I leave it at the end! How do we fix it, to prevent restarting the process each time we upload a file?
Create the configuration file in the root folder:
ecosystem.config.js
module.exports = {
apps: [
{
name: "DEH",
script: "./server/index.js",
interpreter: "./node_modules/#babel/node/bin/babel-node.js",
interpreter_args: "./node_modules/#babel/node/bin/babel-node",
watch: ["server"],
watch_delay: 1000,
ignore_watch: [
"node_modules",
"./server/src/files",
"./server/src/pdf",
],
watch_options: {
followSymlinks: false,
},
},
],
};
To run it:
pm2 start ecosystem.config.js
Note that ignore_watch has an array with elements that will not be checked.
The interpreter points to the folder where it was installed locally: if you don't specify it you will have to install babel-node globally (I don't recommend it)
I'm having an issue with pm2.
If I start my app with
cd /my/path
pm2 start server.js
everything works correctly, but if I use an ecosystem.config.js file, then it gives the following error
pm2 start ecosystem.config.js
Error: Cannot find module '/my/path/server.js'\n at Function.Module._resolveFilename
my ecosystem file is configured as follows
module.exports = {
"apps": [{
"name": "MyApp",
"script": "server.js",
"cwd": "/my/path"
}]
}
I tried uninstalling pm2, updating npm and reinstalling node_modules, but still I can't understand why it says a module is missing in my file (expecially because it works when not using the ecosystem file)
Ensure that the ecosystem.config.js is in the root of your project as well as your server entry file (server.js) and specify your script as below:
module.exports = {
"apps": [{
"name": "MyApp",
"script": "./server.js"
}]
}
Reading from https://pm2.keymetrics.io/docs/usage/application-declaration/
Field Type Example Description
name (string) “my-api” application name (default to script filename without extension)
script (string) ”./api/app.js” script path relative to pm2 start
cwd (string) “/var/www/” the directory from which your app will be launched
I would try:
with "script": "./server.js" (kind of strange, I know)
remove the cwd parameter and setting ./my/path/server.js
Try this command: pm2 start src/server.js
or other folder name before server file name
I've been trying to get nodemon to ignore my public folder for a while now, and not having much luck...
I've created nodemon.json in my root directory next to my package.json. In this I just have the following commands...
{
"name": "nodemon",
"homepage": "http://nodemon.io",
"nodemonConfig": {
"ignore": ["public/*"],
"delay": "2500"
}
}
I've tried many iterations of the way the directory is written above, but having no luck - as soon as a file is altered in the public folder the server resets.
Also, the public folder is one level up from the root, definitely in the right location.
Any ideas?! I don't particularly want to use flags each time I run nodemon if I can avoid it as thats a faff... Potentially its not reading my nodemon.json file...?
Thanks!
this worked for me, get rid of the " * "
in the "ignore": ["public/*"],
This code is written in the nodemon.json config folder, so create it and write this code in it, nodemon will see that folder and read the config, more about this on nodemon npm page
{
"verbose": true,
"ignore": ["public/", "README"]
}