I want to implement an error handler in my Parse app, but I don't know how should I declare and place my middleware.
If I use the app.js file in the root of the application I know that I can use a declared middleware with app.use, but as it is an error middleware, it should be placed at the end of my routes, I'm confused if I should place this on the app.js, which I think that is logically placed by parse on top of my Cloud functions, or if I should place the file in the main.js, where are my functions imports.
If I place the middleware at the end of the app.js, I receive this error:
TypeError: Cannot read properties of undefined (reading 'headersSent')
at headersSent (/usr/src/app/node_modules/finalhandler/index.js:256:21)
at /usr/src/app/node_modules/finalhandler/index.js:92:17
at /usr/src/app/node_modules/express/lib/router/index.js:646:15
at next (/usr/src/app/node_modules/express/lib/router/index.js:216:14)
at Function.handle (/usr/src/app/node_modules/express/lib/router/index.js:175:3)
at Function.handle (/usr/src/app/node_modules/express/lib/application.js:181:10)
at app (/usr/src/app/node_modules/express/lib/express.js:39:9)
at Object.<anonymous> (/usr/src/app/data/cloud/app.js:44:1)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
I'm interested in handle errors through a middleware because of architecture reasons, and the flow should throw errors catched in try/catch declarations, to send the error object to the next middleware.
It's important to mention that I'm using back4app, so I only have access to files in the /cloud directory. I don't have access to the rest of the express app.
Related
I'm trying to deploy my api to Google Cloud Functions, and I'm getting this:
EROFS: read-only file system, mkdir '/user_code/uploads'
⚠ functions[post]: Deployment error. Function load error:
Code in file index.js can't be loaded. Is there a syntax error in your code?
Detailed stack trace: Error: EROFS: read-only file system, mkdir '/user_code/uploads'
at Error (native)
at Object.fs.mkdirSync (fs.js:932:18)
at Function.sync (/user_code/node_modules/multer/node_modules/mkdirp/index.js:71:13)
at new DiskStorage (/user_code/node_modules/multer/storage/disk.js:21:12)
at module.exports (/user_code/node_modules/multer/storage/disk.js:65:10)
at new Multer (/user_code/node_modules/multer/index.js:15:20)
at multer (/user_code/node_modules/multer/index.js:95:12)
at Object.<anonymous> (/user_code/api/user.js:105:46)
at Module._compile (module.js:577:32)
at Object.Module._extensions..js (module.js:586:10)
Everything in the Cloud Functions runtime is read-only except for os.tmpdir() (which is likely going to be /tmp, but you shouldn't assume that). If you have any code (in api/user.js for example) that attempt to write anywhere else, it'll error.
Same issue for python, but putting this for clarity. My error -
File "/env/local/lib/python3.7/site-packages/google/cloud/storage/blob.py", line 753, in download_to_filename
with open(filename, "wb") as file_obj:
OSError: [Errno 30] Read-only file system: 'testFile.zip'
Get the temp directory as follows (usually /tmp):
import tempfile
tmpdir = tempfile.gettempdir()
Googles documentation can be found here.
While Cloud Storage is the recommended solution for reading and writing files in App Engine, if your app only needs to write temporary files, you can use standard Python 3.7 methods to write files to a directory named /tmp.
All files in this directory are stored in the instance's RAM, therefore writing to /tmp takes up system memory. In addition, files in the /tmp directory are only available to the app instance that created the files. When the instance is deleted, the temporary files are deleted.
Gen1 cloud functions are read-only systems. However, Gen2 cloud functions aren't. I'd recommend changing your function to Gen2
(be careful, this might interfere with another config as gen 2 can be considered as a cloud run)
I have an express app calling an Angular2 app. I am consistently getting Error: (SystemJS) Unexpected token < and searching for a bit has led me to find that this is typically due to the wrong content-type being returned.
I found that app.module.js is returning html and not .js, is this what is throwing the code off?
Error: (SystemJS) Unexpected token <
The error in detail is:
dashboard:21 Error: (SystemJS) Unexpected token <
SyntaxError: Unexpected token <
at eval (<anonymous>)
at Object.eval (http://localhost:3000/main.js:3:20)
at eval (http://localhost:3000/main.js:8:4)
at eval (http://localhost:3000/main.js:9:3)
Evaluating http://localhost:3000/app/app.module.js
Evaluating http://localhost:3000/main.js
Error loading http://localhost:3000/main.js
at eval (<anonymous>)
at Object.eval (http://localhost:3000/main.js:3:20)
at eval (http://localhost:3000/main.js:8:4)
at eval (http://localhost:3000/main.js:9:3)
Evaluating http://localhost:3000/app/app.module.js
Evaluating http://localhost:3000/main.js
Error loading http://localhost:3000/main.js
Here is a snapshot of it as dev tools for network:
This is what my express looks like:
app.use('/styles.css', express.static(path.join(__dirname, '../angular/src/styles.css')));
app.use('/systemjs.config.js', express.static(path.join(__dirname, '../angular/src/systemjs.config.js')));
app.use('/systemjs-angular-loader.js', express.static(path.join(__dirname, '../angular/src/systemjs-angular-loader.js')));
app.use('/systemjs.config.extras.js', express.static(path.join(__dirname, '../angular/src/systemjs.config.extras.js')));
app.use('/main.js.map', express.static(path.join(__dirname, '../angular/src/main.js.map')));
app.use('/main.js', express.static(path.join(__dirname, '../angular/src/main.js')));
app.use(express.static(path.join(__dirname, '../angular')));
app.use('/api/', auditlog);
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../angular/src/index.html'));
});
Your express app is set up to serve index.html to any request, so you'll get that file back when you might usually get a 404. Given the error, I'm guessing it likely that you have a missing JS file, or possibly some missing SystemJS module mappings. You can use the network inspector to find the missing file by looking in the preview tab of the request.
Consuming all routes is handy for Angular because of its routing, but it might be helpful to check if the client is requesting text/html before returning the app, returning 404 if not - This will make it easier to identify missing JS files on the client side.
The client I am working with at the moment has requested I build a SSR React app which includes i18n. To solve this problem I tried to use NextJS and i18Next.
I managed to get react and apollo linked up without issue but when I tried to connect next + i18n I keep getting a very unhelpful 'an unexpected error' occured, error.
Inspecting the console I also see the following:
Cannot read property 'prototype' of undefined
TypeError: Cannot read property 'prototype' of undefined
at Object.inherits (http://localhost:3000/_next/1515592576452/page/index.js:36080:46)
at Object.<anonymous> (http://localhost:3000/_next/1515592576452/page/index.js:30216:6)
at Object.<anonymous> (http://localhost:3000/_next/1515592576452/page/index.js:30440:30)
at __webpack_require__ (http://localhost:3000/_next/1515592576452/manifest.js:714:31)
at fn (http://localhost:3000/_next/1515592576452/manifest.js:117:20)
at Object.<anonymous> (http://localhost:3000/_next/1515592576452/page/index.js:28246:12)
at Object.module.exports.exports.byteLength (http://localhost:3000/_next/1515592576452/page/index.js:28507:30)
at __webpack_require__ (http://localhost:3000/_next/1515592576452/manifest.js:714:31)
at fn (http://localhost:3000/_next/1515592576452/manifest.js:117:20)
at Object.<anonymous> (http://localhost:3000/_next/1515592576452/page/index.js:24501:18)
Also this warning but i'm not sure it's related
warning.js?e7f4ca8:33 Warning: Expected server HTML to contain a matching <div> in <div>.
I've also noticed that in ./libs/withData.js there is the following line:
enter code herestatic displayName = WithData(${getComponentDisplayName(
ComposedComponent
)})
My editor flags that as an invalid token but this code came from the nextjs example and there is no issue on that repo which suggests it's a typo.
I've included all the code that pertains to this issue in the following codepen project.
https://codepen.io/foxleigh81/project/editor/DnYJWB
OK I've sort of worked it out.
In a nutshell. I was thinking about this all wrong. I was thinking I was adding next to my react app, whereas the reality is more like I'm putting my react app inside next. I took it apart and rebuilt it from scratch, assembling the examples on the nextjs repo and it all worked.
I'm using node v4.8.0 (npm v3.10.10) and in my service code i'm generating a hash using Crypto something like below,
hash = crypto.createHash('sha256')
.update(val1+val2+JSON.stringify(val3), 'utf8')
.digest('hex');
when i build the app it fails with the below error, it was working few weeks back and all of a sudden ended getting the error. Any idea how could i overcome this one, this seems to be a viable solution https://github.com/webpack/webpack/issues/4072 but not getting it fully though since i'm not explicitly using web pack as i'm dealing with an API and not a client app
Error:
crypto.js:70
this._handle.update(data, encoding);
^
TypeError: Not a string or buffer
at TypeError (native)
at Hash.update (crypto.js:70:16)
at Object.<anonymous> (/srv/approot/node_modules/v8flags/index.js:14:81)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
......
It works for me.
So I guess one of your data is null or something else than a string or buffer.
I am running an Express application on Nodejitsu. It's Node 0.8 and Express 3.4.0. The application works absolutely fine locally, but one of the routes has stopped working and returns the following error when I push to Nodejitsu:
Express
500 Error: Unknown encoding
at Buffer.toString (buffer.js:440:13)
at Object.fs.readFileSync (fs.js:241:33)
at handleCache (/opt/run/snapshot/package/node_modules/ejs/lib/ejs.js:65:21)
at exports.renderFile (/opt/run/snapshot/package/node_modules/ejs/lib/ejs.js:206:14)
at View.exports.__express [as engine] (/opt/run/snapshot/package/node_modules/ejs/lib/ejs.js:510:22)
at View.render (/opt/run/snapshot/package/node_modules/express/lib/view.js:76:8)
at Function.app.render (/opt/run/snapshot/package/node_modules/express/lib/application.js:504:10)
at ServerResponse.res.render (/opt/run/snapshot/package/node_modules/express/lib/response.js:798:7)
at exports.m2 (/opt/run/snapshot/package/routes/login.js:27:9)
at callbacks (/opt/run/snapshot/package/node_modules/express/lib/router/index.js:164:37)
I have nt changed anything about the route. I added a new route to the sme routing file which works, but should not affect this route). I haven't changes the view template in any way, or done anything I can think of to change the encoding. Given that Nodejitsu just takes my package.json and installs all the same modules, everything about the app should be identical.
I'm not sure how to track down the issue since the Nodejitsu logs are identical to the output error and I can't reproduce locally. I tried commenting out the new route, and it had no effect.