NestJS Multer set dest to an url - api

Hello!
So, when i upload a file with nestjs and multer, i want to set the dest in the module to an url, but if i do that, then it gives me an error:
EINVAL: invalid argument, mkdir 'C:\Users\almak\Desktop\Chatenium2\chatenium-server\http:\localhost'
Can you help me why? Thanks, and also is there any way to prevent nestjs from renaming and removing the file extension from the file (test.png => 03ebe1f47494378fee61196c0524afaf )
Heres the code:
Module:
MulterModule.register({
dest: process.env.CDN_URL,
}),
Controller:
#Post("uploadImg")
#UseInterceptors(FileInterceptor("file"))
async uploadedFile(#UploadedFile() file) {
return file;
}

dest by default is a local directory for the server to access via the fs module. If you need to upload the file to another server from your server, you should use a different storage type.
As for the renaming of the file, that's also multer's default, you can pass options to the FileInterceptor according to multer's documentation to change how the file gets handled.

Related

Webpack dev server serves wasm with wrong mime type

I'm using Vue 2 (but I think that my question isn't Vue-specific). The application uses a WebAssembly library of mine that I've installed with npm install [directory_name]. When I run npm run serve, it serves the .wasm file as text/html instead of application/wasm.
I add this to vue.config.js:
module.exports = {
// ...
devServer: {
mimeTypes: { 'application/wasm': ['wasm'] },
},
};
But in that case, I get this:
Error: Attempt to change mapping for "wasm" extension from "application/wasm" to "application/wasm". Pass force=true to allow this, otherwise remove "wasm" from the list of extensions for "application/wasm".
Is it possible cli-vue-service/webpack-dev-server already knows about .wasm files, and I've understood something wrong about how we install libraries with npm install?
I'm answering my own question.
It's tricky (assuming it's possible) to get webpack to load the .wasm file because of the way webpack works. What I did was add -s "SINGLE_FILE=1" to emcc's options so that it does not produce a .wasm file; instead, it embeds the wasm in the glue .js file.
Btw I came here after getting this error:
Uncaught (in promise) TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'.
I had a typo in my wasm fetch URL and I was fetching a file that didn't exist, but it was giving me this mime type error. Once I fixed the typo, everything worked. I didn't have to add the mime type to the webpack config. (maybe they fixed it since you posted in February.) I'm using the Vue development server.

Vue.js: load js file based on environment

I have some js file which is generated by aws cognito, and its content is different based on environment.
As I should not modify it manually, so vue's environment variables may not work for me.
How should I dynamically load the js file based on the environment?
I use following command to run my server on local
vue-cli-service serve --mode=dev
I located the file under /environment/dev/aws-exports.js
const awsmobile = {
"aws_project_region": "ap-northeast-1",
... some more json...
};
export default awsmobile;
and load in the main.js
import config from "./environment/dev/aws-exports"
Amplify.configure(config);
How should I modify my code so that it can load it based on --mode?
I am not sure if this is exactly what you are looking for but I'll try to help:
To dynamically load the js file based on the environment you can create a function that returns an import:
function getConfig(mode) {
return import(`./environment/${mode}/aws-exports`) // import returns a Promise
}
Note: remember that import is a Promise so you might need to await it.

chunks file urls problem in laravel and vuejs

i am developing a web application with laravel and vue
i deploy application in this route in host:
https://novintech.info/panel
anything is true but chunks file have wrong path and refer to:
https://novintech.info
for example:
https://novintech.info/js/home.js //this is chunk file
how to refrence this file to:
https://novintech.info/panel/js/home.js
please help. thanks
problem solved by adding output.publicPath property in webpackconfig
mix.webpackConfig({
output: {
publicPath: 'https://novintech.info/info/public/',
}
});
Once you ran the production script, go to the file generated folder public/, open the file app.js in an editor.
Research for js/chunk and replace it by info/public/js/chunk.

Can't find static assets from express/npm module

tldr;
My project is an NPM module that is used by an ExpressJS server. The server needs to specify an endpoint and my module will do the rest. How do I get my module to load the correct html page and grab the correct js/css files from the correct path?
The Problem
I'm running into a problem where I can see the directory structure of the site, using the serveIndex library, and all the files are in their correct directories but for some reason when I try to load any of the files, whether from the serveIndex view or from the actual endpoint where it should load, I get nothing but 404 errors.
Here's an example if someone wanted to use this NPM module from their project.
app.js (their server)
const express = require('express')
const { adminAreaConfig } = require('express-admin-area')
const app = express()
const adminArea = adminAreaConfig(express) // my module being passed the "express" library
app.use('/admin', adminArea) // specify a URL to use my module
app.listen(3000, () => console.log('\n\nServer Online\n\n'))
Here's an image of my projects dir structure after it's been built.
Going off of a console.log(__dirname), which returns <long path string>/express-admin-area/build/src, I then tell my module, using the express reference passed by the actual server in the code above, to look in the views directory with
... import libraries etc ...
const adminAreaConfig = express => {
const adminArea = express.Router()
adminArea.use('/', express.static(__dirname + '/views') // sets my modules views to the "http://localhost:3000/admin" path
adminArea.use('/dirs', serveIndex(__dirname)) // will get into this later
... some other stuff like exports etc ...
This then attempts to load the index.html file in the express-admin-area/build/src/views directory but fails because it can't locate the CSS and JS files inside express-admin-area/build/src/views/static/css or .../js.
First, I know it fails because instead of looking for http://localhost:3000/admin/static/css/styles.css it looks for http://localhost:3000/static/css/styles.css, so that's another problem I need to solve entirely.
Second, looking back at the small code sample above, adminArea.use('/dirs', serveIndex(__dirname)), I'm using the serveIndex library in an attempt to view the directory structure. If I go to http://localhost:3000/admin/dirs I get the correct directories and files in the browser
But now, if I try to view an actual file I'm met with the error Cannot GET /admin/dir/main.js for example if I were to go to http://localhost:3000/admin/dir/main.js, but I can continue going deeper in the directories if I wanted such as the controllers or routes directories from the image.
What I want
I need a way to get these static assets to load. If I point my module to a basic html page with a simple <h1>Hello, World!</h1> then that's what Ill get but trying to load any outside scripts/stylesheets is when I get the 404 errors and nothing loads.
I'll be answering my own question.
The solution is actually pretty simple. The view layer of this module is handled by React, CRA to be specific. CRA will look for some specific environment variables, one of them being PUBLIC_URL. All I had to do was
Create a .env file in the root directory of my CRA
add PUBLIC_URL="/admin"
Afterward, it's just rebuilding the project, yarn build, and reset the server. CRA will then look at http://localhost:3000/admin/static/... instead of http://localhost:3000/static/... for static assets.

AWS Lambda package-deployed functions require() of a relative path, not found

I have a zip file containing the following structure (this is the root of the archive, not nested in a top-level folder, which I understand is a common cause of errors for aws-s3-lambda deployments):
- support/
- shared.js
- one.js
- two.js
and then in one.js and two.js:
var shared = require("./support/shared");
// ...
When I run this code locally, it works. I use the aws-sdk to upload the zip file to AWS-S3 and then use aws.lambda.createFunction() to create a function with that name and handler and everything. The created function DOES show up in my Lambda dashboard, but when I test it, I get "Cannot find module './support/shared'". I have also tried var shared = require("./support/shared.js"); and that gives "Cannot find module './support/shared.js'".
This is for runtime node6.10. The filename cases are correct for case-sensitive lambda.
Shouldn't this work?? What's the gotcha?
Is there a way to verify the file structure that Lambda is working in to show that the additional ./support/shared.js file actually made it to the working directory or whatever it uses?
The gotcha is that the zip file created on a windows machine has the wrong chmod permissions set in it for when AWS unpacks it. The files are there, but inaccessible but node just gives a generic warning about not found instead of that the folder access is denied.