I'm trying to use SQL server and nodejs with my HTML file.
I'm connecting with my database with nodejs using mssql and hapijs, and i can obtain my table in json format, but i dont know how to obtain this information on my HTML.
Also, how can i make the server update from HTML?
Here is my nodejs code:
var http = require("http");
getData = function(callback){
var sql = require('mssql');
//Data Connection
sql.connect("mssql://username:password#localhost\\SQLEXPRESS/CrudExample?encrypt:true").then(function() {
// Query
new sql.Request().query('select * from MOCK_DATA').then(function(recordset) {
//console.dir(recordset);
callback(recordset)
response.writeHead(200, {"Content-Length": results.length});
response.writeHead(200, {"Content-Type": "application/json"});
response.end(results);
}).catch(function(err) {
// ... query error checks
});
}).catch(function(err) {
// ... connect error checks
console.log('Error en la consulta')
console.log(err)
});
}
const Hapi = require('hapi');
const Path = require('path');
// Create a server with a host and port
const server = new Hapi.Server({
connections: {
routes: {
files: {
relativeTo: Path.join(__dirname, 'public')
}
}
}
});
server.connection({
host: 'localhost',
port: 8000
});
// Add the route
// Start the server
server.register(require('inert'), (err) => {
if (err) {
throw err;
}
server.route({
method: 'GET',
path:'/getTable',
handler: function (request, reply) {
getData(function(r) {return reply(r)})
}
});
server.route({
method: 'GET',
path:'/',
handler: function (request, reply) {
reply("hello world")
}
});
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
});
You can use handlebars as a view engine
refer to http://hapijs.com/tutorials/views
Related
I am trying to use the Get method from the code below. I can use the Post method to post new instances to the database but my Get method is not working. When I tried to use the Get method I encountered the "AxiosError: Request failed with status code 404" error.
This is my code that contains the Get and Post methods:
const express = require('express');
const mongoose = require('mongoose');
const { ObjectId } = require('mongodb');
const { connectToDb, getDb, URI } = require('./db');
const Root = require('../models/Root');
const port = process.env.PORT || 7000;
const URL = 'http://localhost:7000'
const axios = require('axios');
// init & middleware
const app = express();
const router = express.Router();
app.use(express.json());
mongoose.set('strictQuery', false);
mongoose.set('bufferCommands', false);
let db
connectToDb((err) => {
if (!err) {
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
}
});
mongoose.connect(URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// POST
app.post('/roots', async (req, res) => {
const { root_id, node_id, name } = req.body;
if (!root_id || !node_id || !name) {
return res
.status(400).send({ error: 'Please provide all required fields' });
}
const root = new Root({ root_id, node_id, name });
try {
const savedRoot = await root.save();
res.send(root);
} catch (err) {
//console.error('Error saving root:', err);
res.status(400).send(err);
}
});
// GET
app.get('/roots/:root_id', async (req, res) => {
try {
const response = await axios.get(
`${URL}/roots?filter={"where":{"root_id":${req.params.root_id}}}`
);
res.status(200).json(response.data);
} catch (err) {
console.error('Error getting root:', err);
res.status(400).send(err);
// res.status(500).json({ error: 'Could not fetch the root' });
}
});
// DELETE
app.delete('/roots/:root_id', async (req, res) => {
try {
await axios.delete(`${URL}/roots/${req.params.root_id}`);
res.status(200).json({ message: 'Root deleted successfully' });
} catch (err) {
console.error('Error getting root:', err);
res.status(400).send(err);
// res.status(500).json({ error: 'Could not delete the root' });
}
// Call to a method to delete all children nodes of the tree in the Node tables
});
// PATCH
app.patch('/roots/:root_id', async (req, res) => {
try {
const response = await axios.patch(
`${URL}/roots/${req.params.root_id}`,
req.body
);
res.status(200).json(response.data);
} catch (err) {
res.status(500).json({ error: 'Could not update the root' });
}
});
I use this code to connect to the database:
// Use this file to connect to database - easy to switch between local and cloud for testing
const{MongoClient} = require('mongodb')
let dbConnection
// Connect to local database
let URI = 'mongodb://127.0.0.1:27017/PM_AI'
module.exports = {
connectToDb: (cb) => {
MongoClient.connect(URI)
// MongoClient.connect(cloudURI)
.then((client) => {
dbConnection = client.db()
return cb()
})
.catch(err => {
console.log(err)
return cb(err)
})
},
getDb: () => dbConnection,
URI
}
ERROR LOG for the error that I encounter:
{
"message": "Request failed with status code 404",
"name": "AxiosError",
"stack": "AxiosError: Request failed with status code 404\n at settle (D:\\CSDS_395_Project\\AI-PM\\node_modules\\axios\\dist\\node\\axios.cjs:1900:12)\n at IncomingMessage.handleStreamEnd (D:\\CSDS_395_Project\\AI-PM\\node_modules\\axios\\dist\\node\\axios.cjs:2944:11)\n at IncomingMessage.emit (node:events:525:35)\n at endReadableNT (node:internal/streams/readable:1359:12)\n at process.processTicksAndRejections (node:internal/process/task_queues:82:21)",
"config": {
"transitional": {
"silentJSONParsing": true,
"forcedJSONParsing": true,
"clarifyTimeoutError": false
},
"adapter": [
"xhr",
"http"
],
"transformRequest": [
null
],
"transformResponse": [
null
],
"timeout": 0,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
"maxContentLength": -1,
"maxBodyLength": -1,
"env": {},
"headers": {
"Accept": "application/json, text/plain, */*",
"User-Agent": "axios/1.3.3",
"Accept-Encoding": "gzip, compress, deflate, br"
},
"method": "get",
"url": "http://localhost:7000/roots?filter={\"where\":{\"root_id\":1}}"
},
"code": "ERR_BAD_REQUEST",
"status": 404
}
The URL that I use to test my method in Postman is http://localhost:7000/roots/1.
Please let me know what am I doing wrong with my code here.
Thank you very much!
In your expressjs server file, the url you are using in mongoose.connect() refers to the expressjs server itself instead of localhost mongodb instance
So in your server.js/app.js or whatever is your main expressjs server file,
const MONGO_URL = 'mongodb://127.0.0.1:27017/PM_AI'
I can also see that you are using both mongo client and mongoose which I don't understand why... You only need one of these libaries to connect to mongodb from your backend
Also your code is pretty messed up so I've made the following changes
No need to use mongoose strict query and other configurations, simply using mongoose.connect() in latest mongoose version is enough. As mongodb connection establishes, you can launch your server
In terminal, write npm install dotenv. It is a package that is used to access variables in .env file, without it your server won't work properly
I've removed mongo client as it is not needed, simply using mongoose is enough
I don't know why you are making axios requests to your own server. This axios thing is what is causing 404 error. You should use axios only when you need to make api calls from frontend, or make api calls from your backend to some other backend server. For your own server, you should always prefer using a controller function for every route otherwise you will get 404 error. By controller function, I mean instead of axios.get, you need to execute mongoModel.delete() instead of axios.delete() or return mongoModel.findById() instead of axios.get()
For mongodb connection, use MONGO_URL and for connecting your own server, use URL
So the final version of your code should look like:
const express = require('express');
const mongoose = require('mongoose');
const { ObjectId } = require('mongodb');
const Root = require('../models/Root');
const MONGO_URL = 'mongodb://127.0.0.1:27017/PM_AI'
const axios = require('axios');
// For environmental variables in .env file
const dotenv = require("dotenv")
dotenv.config()
// init & middleware
const app = express();
const router = express.Router();
app.use(express.json());
const port = process.env.PORT || 7000
const URL = `http://localhost:${port}`
mongoose.connect(MONGO_URL).then(() => {
console.log("Mongodb connected")
app.listen(port,() =>
{console.log("Server started") }
});
// POST
app.post('/roots', async (req, res) => {
const { root_id, node_id, name } = req.body;
if (!root_id || !node_id || !name) {
return res
.status(400).send({ error: 'Please provide all required fields' });
}
const root = new Root({ root_id, node_id, name });
try {
const savedRoot = await root.save();
res.send(root);
} catch (err) {
//console.error('Error saving root:', err);
res.status(400).send(err);
}
});
// GET
app.get('/roots/:root_id', async (req, res) => {
try {
const response = await axios.get(
`${URL}/roots?filter={"where":{"root_id":${req.params.root_id}}}`
);
res.status(200).json(response.data);
} catch (err) {
console.error('Error getting root:', err);
res.status(400).send(err);
// res.status(500).json({ error: 'Could not fetch the root' });
}
});
// DELETE
app.delete('/roots/:root_id', async (req, res) => {
try {
await axios.delete(`${URL}/roots/${req.params.root_id}`);
res.status(200).json({ message: 'Root deleted successfully' });
} catch (err) {
console.error('Error getting root:', err);
res.status(400).send(err);
// res.status(500).json({ error: 'Could not delete the root' });
}
// Call to a method to delete all children nodes of the tree in the Node tables
});
// PATCH
app.patch('/roots/:root_id', async (req, res) => {
try {
const response = await axios.patch(
`${URL}/roots/${req.params.root_id}`,
req.body
);
res.status(200).json(response.data);
} catch (err) {
res.status(500).json({ error: 'Could not update the root' });
}
});
i'm trying to learn how to build an api with Node and Express.js. I've found the next step by step: click here
And created a very similar version but with my data:
var express = require("express");
var bodyParser = require("body-parser");
var sql = require("mssql");
var app = express();
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
next();
});
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
var dbConfig = {
user: "myUser",
password: "myPass",
server: "myServer",
database: "MyDB"
};
var executeQuery = function(res, query){
sql.connect(dbConfig, function (err) {
if (err) {
console.log("Error al conectarse a la base :- " + err);
res.send(err);
}
else {
// create Request object
var request = new sql.Request();
// query to the database
request.query(query, function (err, res) {
if (err) {
console.log("Error al correr query en la base :- " + err);
res.send(err);
}
else {
res.send(res);
}
});
}
});
}
//GET API
app.get("/api/ApiRequestData", function(req, res){
var query = "select * from [RequestData]";
executeQuery (res, query);
});
After create the server.js doc, executed with npm install and run with node server.js, i opened on postman using the next url: http://localhost:8080/api/ApiRequestData and get an error message: "Could not get any response". On the Node command prompt i get the message:
TypeError: res.send is not a function
at C:\Users\API\server.js:43:44
at C:\Users\API\node_modules\mssql\lib\main.js:1588:20
at Request.userCallback (C:\Users\API\node_modules\mssql\lib\tedious.js:853:61)
at Request.callback (C:\Users\API\node_modules\tedious\lib\request.js:33:27)
at Connection.message (C:\Users\API\node_modules\tedious\lib\connection.js:1179:27)
at Connection.dispatchEvent (C:\Users\API\node_modules\tedious\lib\connection.js:519:45)
at MessageIO. (C:\Users\API\node_modules\tedious\lib\connection.js:439:23)
at emitNone (events.js:106:13)
at MessageIO.emit (events.js:208:7)
at ReadablePacketStream. (C:\Users\API\node_modules\tedious\lib\message-io.js:92:15)
Someone knows why shows this message?
Hope you can help me.
You are shadowing res from line var executeQuery = function(res, query){... with res from line request.query(query, function (err, res) {.... Just rename the last res to something else and you won't get this error:
request.query(query, function (err, result) {
if (err) {
console.log("Error al correr query en la base :- " + err);
res.send(err);
}
else {
res.send(result);
}
});
You can use below query for fetching the records with hard coded query like.
I used same for my application.
sql.connect(config).then(() => {
return sql.query`select * from [dbo].[Customer]`
}).then(result => {
console.log(result)
}).catch(err => {
console.log(err);
})
Fetch the result using store procedure.
sql.connect(config).then(pool => {
return pool.request().input('Customerid', sql.Int, 2).execute("GetCustomerbyId")
}).then(result => {
console.log(result)
}).catch(err => {
console.log(err);
})
With express.js i can do this
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var port = 9200;
var path = require('path');
var options = {
index: "html/index.html"
};
app.use('/', express.static('res', options));
server.listen(port);
console.log("Listening on port " + port);
How do i achieve the same thing using Hapi.js?
I have tried some things also with inertjs but I can't seem to find the correct way. Does anyone have experience with it?
Found my way to this implementation, but im getting TypeErrors:
TypeError: Cannot read property 'register' of undefined
server.register(require('inert'), (err) => {
if (err) {
throw err;
}
server.route({
method: 'GET',
path: '/index',
handler: function (request, reply) {
reply.file('C:/blabla/html/index.html');
}
});
});
Straight from the doc
In detail, you just have to call reply.file()
This will not work with hapi 17 because of the major change in the request system
server.route({
method: 'GET',
path: '/picture.jpg',
handler: function (request, reply) {
reply.file('/path/to/picture.jpg');
}
});
Fixed by using Hapi#16.x.x and Inert#4.x.x and using Ernest Jones answer.
For anyone dealing with this problem in the future here is my complete change:
const Hapi = require('hapi');
const Inert = require('inert');
const server = new Hapi.Server();
server.connection({port: 9200});
server.register(Inert, (err) => {
if (err) {
throw err;
}
server.route({
method: 'GET',
path: '/index',
handler: function (request, reply) {
reply.file('/res/html/index.html');
}
});
server.route({
path: "/res/{path*}",
method: "GET",
handler: {
directory: {
path: "./res",
listing: false,
index: false
}
}});
});
server.start();
I have my server.js file working. at localhost:8080 it will serve the file i give it from the the corresponding url name like so http://localhost:8080/about.html, as long as the file exists in public/pages. I'm wondering if I can somehow set a wildcard to leave of extensions for all html files in the url so that I don't have to individually specify each file as an alias in the routes like - ['about','about.html'].
Here is my working code -
'use strict';
const Path = require('path');
const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({
port: Number(process.argv[2] || 8080),
host: 'localhost'
});
server.register(require('inert'), (err) => {
if (err) {
throw err;
}
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: 'public/pages',
listing: true
}
},
config: {
state: {
parse: false, // parse and store in request.state
failAction: 'ignore' // may also be 'ignore' or 'log'
}
}
});
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
});
Any help is greatly appreciated, thank you.
I am trying to access a Magento SOAP API (v 1.9.2.4) using npm magento. But data is always null:
var express = require('express');
var app = express();
var MagentoAPI = require('magento');
var magento = new MagentoAPI({
host: 'localhost',
port: 8080,
path: '/magento/api/xmlrpc/',
login: 'mothership',
pass: 'bvZ0k0B02pTjujN'
});
app.get('/', function (req, res) {
res.send('Customers: ');
});
var magentoCallback = function(data) {
console.log('Got data: ' + data);
console.log(data);
};
magento.login(function(err, sessId) {
if (err) {
console.log("Error accessing Magento");
console.log(err);
console.log("Session ID: " + sessId);
return;
}
console.log("Connected to Magento");
magento.core.info(magentoCallback);
});
app.listen(3000, function () {
console.log('Racing on port 3000');
});
today, i was the same problem and after two hours i found the resolution. You need change the magentoCallback function to :
var magentoCallback = function(err, response) {
if (err) {
return console.log(err);
}
console.log("Result: ");
console.log(response)
};