Couldn't upload file using multer in node js + angular js application - javascript

I want to upload a file in a node js + angular js application I defined a directive and a service in my controller like said here Data not posting in multipart form data in angular and node js(File uploading with angular and node js)
It seems like it's not workig since my server is returning 500 internal error
my js file
var express = require('express');
var router = express.Router();
var multer = require('multer');
var upload = multer({ dest: __dirname+'/../../uploads' });
router.post('/fileUpload',upload.single('myFile'),uploadFile);
function uploadFile(req,res,next) {
console.log(req.file); //returning undefined
}
In addition a directory 'uploads' is created but nothing is added to it and when I inspect the browser I find internal server error 500
Any help would be appreciated !

Actually I found a post that solved my problem perfectly since I was dealing with multer, angular js and node js nodejs + multer + angularjs for uploading without redirecting
This is helpful!

Try changing
router.post('/fileUpload',upload.single('myFile'),uploadFile);
to
router.post('/fileUpload',upload.single('file'),uploadFile);

Related

Edit static js file before serving in express js

I am currently serving a static bundle.js containing a react app with express, this is my current code.
var express = require('express');
var app = express();
var fs = require('fs')
app.use(express.static('./build')); // build contains bundle.js file
var port = parseInt(fs.readFileSync("./default_port"));
app.listen(port);
I need to be able to pass information into the bundle.js file, I have not figured out how to do this (I am not able to write to the file in the filesystem level). My hope is that there is a way to load bundle into a variable, edit the variable, then serve it with express, however I cant find any method online to do this. Is there a way to pass information into a static js file in express?

VS Code shows directory structure instead of app.js file while using Live Server

I am working on node(Express). I'm getting my directory structure instead of content. Moreover my browser window isn't getting reloaded too when I save my file. I don't have any index.html file as I don't need it.
P.S. I read many related answers on stackoverflow but all those involved index.html relation somehow
This is the image of my directory structure
var express = require('express');
var app = express();
app.get('/',function(req, res){
res.send("This is Landing page");
});
app.listen(5500, function(){
console.log("Server has started at port 5500");
});
Code from app.js file

Feathers js upload file using raw json in postman

How do we configure feathers js to support form-data ? . Basically my current implementation right now supports raw json , but I have a feature
where I have to upload file to amazon bucket and the only way to upload the file like using postman is to support form-data . Thanks
or like is there a way we can upload file without using form-data ? like using raw in post-man ? (edited)
For those kinds of file-uploads you need an additional middleware to handle the multipart/form-data upload - usually multer is used. Here's some sample code that should help you get started:
const multer = require('multer');
const fileUploadHandler = multer();
// Upload Service with multipart support
app.use('/photos',
// you can define different storage options, the default is to keep the uploaded data in memory
fileUploadHandler.single('filename'),
function(req,res,next){
// the uploaded file is accessible now under req.file
next();
}
);

using JSON data in client side javascript file

Im fairly new to node.js and express. My question is how to correctly retrieve json data in a client side javascript file since im getting a 404 code about the .json file not being found.
my file structure is a generic express structure and the .json file is in the nodeapp folder right below the package.json file and app.js. Im trying to access this file from a javascript file that is saved in public/javascripts folder but can seem to get around it. here is the function im trying to implement in the .js file:
function getJSONData(){
var json;
$.getJSON('/public/web_text.json', function(data){
json = data
});
}
Option 1
You need to setup static files in express, and you do it like this:
app.use(express.static(__dirname + '/public'))
You then can call it from your client (without the public prefix):
$.getJSON('/javascript/web_text.json', function(data){});
https://expressjs.com/en/starter/static-files.html
Option 2
Your other option is to send it via sendFile http://expressjs.com/en/api.html#res.sendFile:
app.get('/public/web_text.json', (req, res) => {
res.sendFile(__dirname + '/my_json_file.json');
});
Then in your client you can call it like this:
$.getJSON('/public/web_text.json', function(data){})

how to serve html file used in react in server

this is folder of code
I use node js and react to build a project I build design using react and controller to get data how I can serve html file in server so when run node server.js it open html page that I created I try this in server. js
app.get('/english', function(req, res) {
res.send(path.join(__dirname + '/index.html'));
});
and when i run this route localhost:3000/english i got this
/home/projects/folder/folder2/myproject/index.html
Read the html file first. You could assign it to a variable first then before sending.

Categories

Resources