I want to delete Folder/File in client side (Using javascript/Jquery/AngularJS1). I was Searching, finally i got using Node.js it can be done in Sitepoint link. Now iam not getting how to set up Node.js fs(File System) with either of the language.(Prefered language is AngularJS1). Looking for solution.
Thanks in advance.
var express = require('express');
var app = express();
var fs = require("fs");
var bodyParser = require('body-parser')
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.get('*', function (req, res) {
console.log(req.path);
var path = req.path;
if(req.path == '/'){
res.sendFile( __dirname + "/" + "index.html" );
}else
{
res.sendFile( __dirname + req.path);
}
});
app.post('/app',function (req, res) {
console.log(req.body)
var action = req.body.action;
var data = req.body.data;
var fname = req.body.fileName;
switch(action) {
case 'upLoad':
function decodeBase64Image(dataString) {
var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),
response = {};
if (matches.length !== 3) {
return new Error('Invalid input string');
}
response.type = matches[1];
response.data = new Buffer(matches[2], 'base64');
return response;
};
var imageBuffer = decodeBase64Image(data);
var newPath = __dirname + "/app/images/" + fname;
fs.writeFile(newPath, imageBuffer.data, function(err) {
res.send({confirm : "uploaded" , filename:fname });
});
default:
}
})
var server = app.listen(8080, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
Hi this is Application Folder which i had used.
In this Server.js file where i included node.js modules and load the application.
Related
Problem:
Trying to get json string but req.body in the post data handler returns undefined. Is it even possible to send json and file in one post request to the server?
Code:
Data sended to the server:
function saveFile(e) {
let info = {titleI: title.value, dirI: dir.value};
let xhr = new XMLHttpRequest();
let formData = new FormData();
let file = e;
formData.append("info", JSON.stringify(info));
formData.append("file", file);
xhr.onreadystatechange = state => { console.log(xhr.status); }
xhr.timeout = 5000;
xhr.open("POST", '/register');
xhr.send(formData);
}
The post data handler:
router.post("/", (req, res) => {
console.log(req.body.info)
var fstream;
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
let dirName = "a"
fs.mkdir("D:/node website/ipgrabber/files/"+dirName+"/", function(err) {
if (err) {
console.log(err)
}
})
fstream = fs.createWriteStream("D:/node website/ipgrabber/files/"+dirName+"/" + filename);
file.pipe(fstream);
fstream.on('close', function () {
res.redirect('back');
});
});
})
This is the main class:
var express = require('express')
var http = require('http')
const mongoose = require('mongoose')
const { json } = require('express')
var cookieParser = require('cookie-parser')
var app = express()
var server = http.createServer(app)
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}));
var busboy = require('connect-busboy');
app.use(busboy());
//error handler
mongoose.connect('mongodb://localhost:27017/grabber', {useNewUrlParser: true, useUnifiedTopology: true});
app.use(cookieParser())
app.set("view engine", "ejs")
app.set('views', __dirname+'/views/html/');
app.use("/js", express.static(__dirname + "/views/js"))
app.use("/css", express.static(__dirname + "/views/css"))
const registerRoute = require("./routes/register")
const grabberRoute = require("./routes/grabber")
app.use("/register", registerRoute)
app.use("/id", grabberRoute)
app.get("/", (req, res) => {
res.redirect("/register")
})
app.use(function (err, req, res, next) {
res.status(400).send("Error code: 2 <hr> This page is currently inaccessible! <br> <a href='/'>GO TO HOMEPAGE</a>")
console.log(err)
})
server.listen(80)
Project resources:
body-parser - 1.19.0
connect-busboy - 0.0.2
cookie-parser - 1.4.5
crypto-js 4.0.0
ejs 3.1.5
express 4.17.1
mongoose 5.11.19
By default, express can't manage to get multipart/form-data correctly.
You have to use a middleware to handle and parse this request.
I recommend to use multer.
In your case, you're using connect-busboy so the info should be handled like this:
req.busboy.on('field', function(key, value, keyTruncated, valueTruncated) {
console.log('your info is here', key, value);
});
I just want to upload file in nodejs using multer. I am using postman to check whether file is doing upload or not. Everytime when I am trying to upload file through postman its showing me error. So below are the code what I have done so far.
my express.js file looks like this
/* ===================
Import Node Modules
=================== */
const express = require('express');
const app = express();
const router = express.Router();
const mongoose = require('mongoose');
const config = require('./database');
const path = require('path');
const appRoot = require('app-root-path') ;
const event = require('./routes/event.router');
const multer = require('multer');
const bodyParser = require('body-parser');
const cors = require('cors');
const port = process.env.PORT || 8080; // Allows heroku to set port
mongoose.Promise = global.Promise;
process.env.NODE_ENV = 'devlopment';
// Database Connection
mongoose.connect(config.uri, {
useMongoClient: true,
}, (err) => {
// Check if database was able to connect
if (err) {
console.log('Could NOT connect to database: ', err); // Return error message
} else {
console.log('Connected to ' + config.db); // Return success message
}
});
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(path.join(appRoot.path, 'dist')));
app.use('/event', event);
// Serve only the static files form the dist directory
app.get('*', (req, res) => {
res.sendFile(path.join(appRoot.path, 'dist/index.html'));
});
// Start Server: Listen on port 8080
app.listen(port, () => {
console.log('Listening on port ' + port + ' in ' + process.env.NODE_ENV + ' mode');
});
event.router.js looks like this
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
const Event = require('../../model/event.model');
var multer = require('multer');
var upload = multer({ dest: './public/uploads/img/',fileFilter:function(req,file,cb){
var ext = file.originalname.split('.').pop();
cb(null, file.fieldname + '-' + Date.now() + '.' + ext);
}
}).single('eventimage');
[![router.post('/', function(req, res, next) {
upload(req, res, function(err) {
console.log('file', req.file);
});
if( !req.file ) {
res.json({ success : false, message: 'You must provide event image!' });
}
else {
res.json({ success : true, message: req.file.eventimage });
}
});
Here is the screenshot for the postman
event.router.js
var express = require('express');
var multer = require('multer');
var route = express.Router();
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/uploads/')
},
filename: function (req, file, cb) {
console.log(file);
let extArray = file.mimetype.split("/");
let extension = extArray[extArray.length - 1];
// cb(null, file.fieldname + '-' + Date.now() + '.' + extension);
cb(null, file.originalname + '-' + Date.now() + '.' + extension);
}
})
var upload = multer({ storage: storage }).single('avatar');
route.post('/', function (req, res) {
upload(req,res, function(err) {
if (err) {
res.send('error uploading file');
}
res.json({
success : true,
message : 'File uploaded!',
file : req.file
});
})
});
module.exports = route;
index.route.js
var fileUpload = require('../routes/fileUpload');
var express = require('express');
var route = express.Router();
route.use('/upload', fileUpload);
module.exports = route;
Now include this index.route.js in your index.js file/app.js file what ever name you have kept.
Don't forget to install all dependencies.
And when uploading image see the name given in .single(), in my case i have written avatar.
See the attachments how to browse your file and what is to be set in headers.
It happens that we are working on a web page that uses a list of users (for example) that we got from a MySql on the form of an array of JSON objects, it worked (we tested it with console.log() )... but that was until I activated Angularjs on the front-end.
The code that I used on the respective files are as follow...
controller.js (angular module):
var app = angular.module("App",[]);
app.controller("AppController",function($scope, $http){
$scope.listofusers = [];
$http.get('/api/users')
.success(function(data){
$scope.listofusers = data;
console.log(data);
})
.error(function(data){
console.log("Error: " + data);
});
api.js:
router.route('/users')
.get(function(request, response) {
usuariosModel.getUsuarios(function(error, data) {
data = JSON.stringify(data);
data = JSON.parse(data);
console.log(data);
response.status(200).json(data);
});
})
.post(function(request, response) {
var datosUsuario = {
username: request.body.username,
password: request.body.password,
email: request.body.email,
permiso: request.body.permiso
};
usuariosModel.insertUsuario(datosUsuario, function(error, data) {
console.log(data);
if (data) {
response.status(200).json({
"Mensaje": "Insertado"
});
} else {
response.status(500).json({
"Mensaje": "Error"
});
}
});
});
routes.js:
var express = require('express');
var app = express();
var path = require('path');
app.get('/api/usuarios', function(req, res) {
res.render('readusers.html');
//res.sendFile(path.resolve('./views/readusers.html'));
//res.sendFile(path.join(__dirname, '../', 'views', 'readusers.html'));
});
app.get('/api/usuarios', function(req, res) {
res.render('index_users')
});
app.put('/api/usuario/:username', function(req, res) {
res.render('edit');
});
module.exports = app;
server.js:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var path = require('path');
var mysql = require('mysql');
var config = require("./models/config.js");
var fs = require('fs'); // manejo filesync
var methodOverride = require("method-override");
var connection = mysql.createConnection(config);
connection.connect(function(error) {
if (error) {
console.error('Error connecting: ' + error.stack);
return;
}
console.log('Connected to server with thread ID ' + connection.threadId);
});
// DB structure
sql_structure = fs.readFileSync('./models/bd.sql').toString();
connection.query(sql_structure, function(error, rows) {
if (error) throw error;
console.log('Base de datos: Estructura completada');
});
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
//app.set("view engine", "html");
//app.use(express.static(__dirname + '/views'));
//app.engine('html', require('ejs').renderFile);
app.use(methodOverride("_method"));
app.use('/', require('./router/routes'));
app.use(express.static(__dirname + '/views'));
//Express
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
//Routes
app.use('/api', require('./router/api'));
app.listen(3000, function() {
console.log("Server on");
});
Hope someone finds what is missing, we have looked through a lot of tutorials and still can't find the mistake.
I'm trying to use nodejs as a layer between my public website and a server on the inside of our network.
I'm using express.js to create a simple REST api. The API endpoint should trigger a request call to a webservice, and return the result.
But the request call inside my .get() function doesn't do anything.
I want to return the result from the nested call to be returned.
Code:
// Dependencies
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
//Port
var port = process.env.PORT || 8080;
// Express
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// Routes
app.get('/invoice', function(req, res){
res.send('Express is workiung on IISNode')
});
app.get('/invoice/api/costumer=:customerId&invoice=:invoiceId', function(req, res){
res.send('Customer ID: ' + req.params.customerId + ' Invoice ID: '+ req.params.invoiceId)
var url = 'http://xxx/invapp/getinvoice?company='+req.params.customerId+'S&customerno=13968&invoiceno='+req.params.invoiceId+'';
request('http://www.google.com', function (error, response, body) {
res.send(body);
})
});
// Start server
app.listen(port);
console.log("API is running on port " + port);
Any suggestions?
You can write in this way
// Dependencies
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
//Port
var port = process.env.PORT || 8080;
// Express
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// Routes
app.get('/invoice', function(req, res){
res.send('Express is working')
});
app.get('/invoice/api/costumer=:customerId&invoice=:invoiceId', function(req, res){
var url = 'http://xxx/invapp/getinvoice?company='+req.params.customerId+'S&customerno=13968&invoiceno='+req.params.invoiceId+'';
request(url, function (error, response, body) {
var data={
body:body,
customerID:req.params.customerId,
invoiceID:req.params.invoiceId
};
res.send(data);
});
});
// Start server
app.listen(port);
console.log("API is running on port " + port);
Please find the snippet I am using. Hope this helps for you as well.
var body="";
function callyourservice(customerId,invoiceId,callback) {
var options = {
uri : url + 'costumer=:customerId&invoice=:invoiceId',
method : 'GET'
}
request(options, function (error, response, body) {
console.log(response);
if (!error && response.statusCode == 200) {
res = body;
}
else {
res = 'Not Found';
}
callback(res);
});
}
callyourservice("customerId value","invoiceId value", function(resp){
body=JSON.stringify(resp);;
});
You can write callyourservice inside a get method from client like
app.get('/'){
}
You can try doing it the node way using pipe
var http = require('http');
http.createServer(function(request, response) {
var proxy = http.createClient(9000, 'localhost')
var proxyRequest = proxy.request(request.method, request.url, request.headers);
proxyRequest.on('response', function (proxyResponse) {
proxyResponse.pipe(response);
});
request.pipe(proxyRequest);
}).listen(8080);
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied to port 9000!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
You can find the reference here
I am trying to make a web service that scrapes data from a site but the one I made is not displaying on my browser. I am using Nodejs with Express framework.
My scores.js works, I can get JSON data from it if I run it on the command prompt but when I run it under the Express framework, nothing shows up on my browser.
This is the code I am using for my server.js
var express = require('express');
var scores = require('./scores');
var app = express();
var port = process.env.PORT || 80;
var router = express.Router();
router.use(function(req, res, next) {
console.log('%s %s', req.method, req.path);
next();
});
router.get('/scores', function(req, res, next) {
res.json({result : scores.getAll()});
});
app.use('/api', router);
app.listen(port);
console.log('Server listening on port ' + port);
And this my code for scores.js
var cheerio = require('cheerio');
var requestify = require('requestify');
var rl = require('readline');
var prompts = rl.createInterface(process.stdin, process.stdout);
exports.getAll = function() {
var jsonResponse;
requestify.get('http://example.com').then(function(response) {
var _response = {
'results' : []
};
var title, link, runtime;
response.getBody();
$ = cheerio.load(response.body);
$('.content').each(function() {
$(this).find('a, img, span').each(function(i, elem) {
if ($(this).is('img'))
title = ($(this).attr('alt'));
if ($(this).is('a') && $(this).attr('class') != 'preview')
link = ($(this).attr('href'));
if ($(this).is('span') && $(this).attr('style') == 'float:left;')
runtime = ($(this).text().replace('time:', ''))
});
_response.results.push({"title" : title, "link" : link, "runtime" : runtime});
});
var jsonResponse = JSON.stringify(_response);
});
return jsonResponse;
}
prompts.question("Hit Enter to exit...\n", function() {
process.exit();
});
You have this line:
res.json({result : scores.getAll()});
in your server.js. However, the scores.js does not have the corresponding method getAll().