This is my app.js generated by express js while installing:
// catch 404 and forward to error handler
app.use(function(req, res, next) {
console.log("coming to this use func");
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
console.log("Dev request sent");
console.log(err.message);
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
console.log("testing this function");
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
// I wrote this code to detect '127.0.0.1:3000/enter' in the url
// correspondingly fetches questions from the database
app.post('/enter', function(req, res, next) {
console.log("Coming here.");
connection.query('SELECT * from questions_table', function(err, rows, fields) {
if (!err) {
console.log('The solution is: ', rows);
var myData = JSON.stringify(rows);
res.render('display', {myData:rows })
}
else {
console.log('Error while performing Query.');
res.render('display', { title: 'The index page!'})
}
});
});
The control never comes to the chunk that I wrote, rather it gets into the if loop that says :
if (app.get('env') === 'development')
ALWAYS and throws some error. Front end is a mere form submission. What am I doing wrong here?
Thanks in advance.
Related
I have created an API in Nodejs. I have tried creating a call which returns HTML to display a site in the browser.
My Call looks like this:
router.get('/displayHTML', checkAccessToken, (req, res, next) => {
if (req.query.data === undefined) {
return res.status(900).json({
message: 'Data does not exist'
});
}
Data.find({ data: req.query.data}).exec()
.then(data => {
if (data.length < 1) {
return res.status(400).json({
message: "Nothing found"
});
}
// I need to return HTML here so the user sees something in his browser
return res.status(200).json({
data: data
});
}).catch(error => {
return res.status(500).json({
message: error
});
});
});
Check the fs_library: https://nodejs.org/docs/v0.3.1/api/fs.html
var http = require('http'),
lib = require('fs');
lib.readFile('./page.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(8000);
});
Trying to make a simple rest service. The rest service is for pulling up a table from a local database. This rest service a want to make available for an android app.
Having trouble getting passed .then block. Tried catching the error but with no success. How do you catch the error if it's going wrong in the first .then
The below piece of code is the db.js, and sets up the connection to the database.
var sqlDb = require("mysql");
var settings = require("../settings");
exports.executeSql = function (sql, callback) {
var conn = new sqlDb.createConnection(settings.dbConfig);
conn.connect()
// !! Error unhandled
.then(function () {
var req = new sqlDb.Request(conn);
req.query(sql)
.then(function (recordset) {
callback(recordset);
})
.catch(function (err) {
console.log(err);
callback(null, err);
});
})
.catch(function (err) {
console.log(err);
callback(null, err);
});
};
After setting up connection the below piece of code is executed. With error handling.
var db = require("../core/db");
exports.getList = function (req, resp) {
db.executeSql("SELECT * FROM employees", function (data, err) {
if (err) {
// throws back error to web
resp.writeHead(500, "Internal Error", { "Content-Type":
"application/json" });
resp.write(JSON.stringify({ data: "ERROR occurred:" + err }));
} else {
resp.writeHead(200, { "Content-Type": "application/json" });
resp.write(JSON.stringify(data));
}
resp.end();
});
};
Made a separated js file for settings such as database. Tested my connection to the db on a same way. Excluded that problem but it keeps returning an error unhandled on the first .then. I'm not familiar with methods till now.
I think I found the problem. new sqlDb.Request(conn); The .Request is not available when using mysql. But how can I fix this
If you catch() an error it will not be caught again without returning a new rejection. Like this:
conn.connect()
.then(function () {
var req = new sqlDb.Request(conn);
// note the "return" here
return req.query(sql)
.then(function (recordset) {
callback(recordset);
})
.catch(function (err) {
console.log(err);
callback(null, err);
// note the line below
return Promise.reject(err)
});
})
.catch(function (err) {
console.log(err);
callback(null, err);
});
};
PS. Do you really need the callback(), why not use the Promise?
I am trying to figure out how I can catch an error when a requested id is not found in the Database records.
My code is:
router.get('/users/:id', function(req, res) {
getId = req.params.id
db.con.query('SELECT * FROM employees where id=?', getId, function(err, results) {
if (err) {
console.log('error in query')
return
} else {
obj = {
id: results[0].id,
name: results[0].name,
location: results[0].location
// error should be cached here when a requested results[0].id is not found
}
res.render('showuser');
}
})
});
In the console, I get the following error when a non-existed id is requested as it should be, however I cannot catch this error programmatically.
throw err; // Rethrow non-MySQL errors
^
ReferenceError: id is not defined
at Query._callback (C:\NodeJS\CRUD\CRUD-4\routes\add.js:21:13)
Node: v8.8.0
Express: v4.15.5
Try this:
try{
obj = {
id: results[0].id,
name: results[0].name,
location: results[0].location
}
}catch(err){
//handle error
}
try {...}catch... is how you handle exceptions in JavaScript. You can read more about it here.
// Error handler
app.use(function(err, req, res, next) {
res.status(500).end(err.message);
});
...
router.get('/users/:id(\\d+)', function(req, res, next) {
var id = req.params.id;
db.con.query('SELECT * FROM employees where id= ?', id, function (err, results) {
if (err)
return next(err);
if (results.length == 0)
return next(new Error('Incorrect id: ' + id));
obj = {
id: results[0].id,
name: results[0].name,
location: results[0].location
}
res.render('showuser', obj);
});
})
Try the code below:
router.get('/product/:id', function(req, res, next){
try {
const { idProduct } = req.body
const [detailProduct] = await Product.detail(idProduct);
if (detailProduct.length === 0) {
return res.status(404).send({
status: "404",
msg: "Not found",
data: null
});
}
return res.status(200).json(detailProduct[0])
} catch (error) {
if (!error.statusCode) {
error.statusCode = 500
}
next(error)
}
}
I am trying to get JSON from an API and store it into a MongoDB database.
Obviously, it doesn't work. My app seems to hang around the point where I try to save the data to the database. Please advise what to do.
Here's my code:
var express = require('express');
var router = express.Router();
var http = require('http');
var mongo = require('mongoskin');
var db = mongo.db("mongodb://localhost:27017/zak", {native_parser : true});
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
var site = 'http://www.vsechnyzakazky.cz/api/v1/zakazka/?format=json&limit=2';
function getData(cb) {
http.get(site, function(res) {
// explicitly treat incoming data as utf8 (avoids issues with multi-byte chars)
res.setEncoding('utf8');
// incrementally capture the incoming response body
var body = '';
res.on('data', function(d) {
body += d;
});
// do whatever we want with the response once it's done
res.on('end', function() {
try {
var parsed = JSON.parse(body);
} catch (err) {
console.error('Unable to parse response as JSON', err);
return cb(err);
}
// pass the relevant data back to the callback
cb(
parsed.objects
);
});
}).on('error', function(err) {
// handle errors with the request itself
console.error('Error with the request:', err.message);
cb(err);
});
}
function writeData (data, allGood){
// couple of visual checks if all looking good before writing to db
console.log('writing');
console.log(typeof data);
console.log(data);
db.collection('zakazky').save(data, function(error, record){
if (error) throw error;
console.log("data saved");
});
}
function allGood(){console.log('all done');}
getData(writeData);
// ---------------------
module.exports = router;
You are calling the save() instead of insert(). Change this part and it will work:
// this should call insert, not save
db.collection('zakazky').insert(data, function(error, record){
if (error) throw error;
console.log("data saved");
});
OK, so I have a problem. If an uncaught exception occurs while I am handling an HTTP request, I have no opportunity to call the end() method on the http.ServerResponse object. Therefore, the server hangs forever and never fulfills the request.
Here's an example:
var express = require('express');
var app = express.createServer();
var reqNum = 0;
app.get('/favicon.ico', function(req, res) {res.send(404);});
app.get('*', function(req, res, next) {
console.log("Request #", ++reqNum, ":", req.url);
next();
});
app.get('/error', function(req, res, next) {
throw new Error("Problem occurred");
});
app.get('/hang', function(req, res, next) {
console.log("In /hang route");
setTimeout(function() {
console.log("In /hang callback");
if(reqNum >= 3)
throw new Error("Problem occurred");
res.send("It worked!");
}, 2000);
});
process.on('uncaughtException', function(err) {
console.log("Uncaught exception!", err);
});
app.listen(8080);
If you visit /error, an exception occurs, but it is caught. The user receives an error message - no problem. If I visit /hang, though, the server will eventually throw an uncaught exception and hang forever. Any subsequent requests for /hang will hang.
This sucks. Any advice for how to fix this issue?
When an uncaught exception occurs, you're in an unclean state. Let the process die and restart it, there's nothing else you can do to safely bring it back to a known-good state. Use forever, it'll restart your process as soon as it dies.
If error is thrown synchronously, express won't stop working, only returning 500.
this.app.get("/error", (request, response) => {
throw new Error("shouldn't stop");
});
If error is thrown asynchronously, express will crash. But according to it's official documentation, there is still a way to recover from it by calling next:
this.app.get("/error", (request, response, next) => {
setTimeout(() => {
try {
throw new Error("shouldn't stop");
} catch (err) {
next(err);
}
}, 0);
});
This will let express do its duty to response with a 500 error.
Use try/catch/finally.
app.get('/hang', function(req, res, next) {
console.log("In /hang route");
setTimeout(function() {
console.log("In /hang callback");
try {
if(reqNum >= 3)
throw new Error("Problem occurred");
} catch (err) {
console.log("There was an error", err);
} finally {
res.send("It worked!");
}
}, 2000);
});