I am building an application using Angular js,Node js and Taffy DB.
I am trying to store the data submitted from front-end to Taffy DB through Node js server.
var express = require('express');
var TAFFY = require('taffy');
var teamlist=TAFFY();
exports.postShareData=function(data,response){
console.log(data);
teamlist.store();
teamlist.insert(data);
console.log(teamlist);
}
But I get "localStorage is not defined".
What may be the problem?How to rectify it.
Please Advice
localStorage is a feature provided by the web browser. In node.js it doesn't exist. Which means if you reference localStorage in a script ran by node.js you will get errors.
you can use npm install localstorage and then var localStorage = require('localStorage') to satisfy the requirement
Related
When I pass the data from node server to front-end (angular 9), I faced the problem.
I made the server side using node and called data from google sheet api.
I tried to get the data from node server to angular and searched on google, but I couldn't find the result.
Here is my code of node server for calling data from googlesheet.
const doc = new GoogleSpreadsheet('1w0aEvmo8H-PPMQEaYWEfN8QSoeGgAKIb9gA0fr4V9VM');
async function accessSpreadSheet() {
await doc.useServiceAccountAuth({
client_email: creds.client_email,
private_key: creds.private_key,
});
await doc.loadInfo(); // loads document properties and worksheets
const sheet = doc.sheetsByIndex[0]; // or use doc.sheetsById[id]
console.log(sheet.title);
console.log(sheet.rowCount);
}
accessSpreadSheet();
In this case, I am not sure how can I pass data of server to angular front-end?
I followed to lots of stackoverflows, but couldn't get them.
Bellow links were I followed.
How do I pass node.js server variables into my angular/html view?
How to send JSON data from node to angular 4
send data from node.js to angular with get()
Please let me know how can I get the data from server into my frontend?
Thanks
If you are using node, the simplest way would be to create an express js api and return the information in an endpoint, though i recommend you learn some basic concepts about APIs and client\server http communication before you start your implementation
I'm a beginner to Node.js and I'm currently building a Node.js program that accesses and queries a Microsoft Azure SQL database with the "tedious" module (see code below) and puts the data onto a html webpage. I want to run this code in a browser so I used browserify to bundle the modules together. However, when this code is run in Google Chrome, the following error is returned: require is not defined. Is there a fix? Is it even possible to use the tedious module in Chrome? If it isn't possible, do I need to use an intermediate server between the Node.js application and the webpage?
var Connection = require('tedious').Connection;
var config = {
userName: 'hackmatch',
password: 'hackvalley123!',
server: 'hackmatch.database.windows.net',
options: {encrypt: true, database: 'AdventureWorks'}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
});
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
Thanks in advance for your help! :)
No. This module can only be used in Node.
tedious depends on the node.js net module to make a connection to the database server. This module has no equivalent on the browser, as web pages cannot make arbitrary network connections.
Even if it were possible to use this module in the browser, it'd be a terrible idea. You'd be allowing anyone on your web site to connect directly to your SQL server and run SQL queries. This can only end badly.
I am making an app using MEAN and ionic framework where nodejs is a middleware to connect to the database(mongoDb). I need to run the nodejs server using node server.js and the app using ionic serve. This is my server.js.
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
mongoose = require('mongoose'),
CohortController =require('./www/server/controller/CohortController');
mongoose.connect('mongodb://localhost:27017/persistent');
app.use(bodyParser());
app.get('/api/cohorts',CohortController.list);
app.post('/api/cohorts',CohortController.create);
app.listen(3000,function(){
console.log('Listening...');
})
Now this is my app.js. I use http://localhost:3000 to get the JSON.
app.controller('CohortController',['$scope','$resource',
function($scope,$resource){
var Cohort=$resource('http://localhost:3000/api/cohorts');
Cohort.query(function(results){
$scope.cohorts=results;
});
$scope.cohorts=[];
$scope.createCohort= function () {
var cohort=new Cohort();
cohort.name=$scope.CohortName;
cohort.id=$scope.CohortId;
cohort.$save(function(result){
$scope.cohorts.push(result);
$scope.CohortName='';
$scope.CohortId='';
});
}
}]);
How can I run the node server when I convert it into a mobile application? How the application will use the API?
You will have to have your Node.js app running on a server which you would then access (from your Ionic app) via it's public IP. So, you wouldn't use http://localhost:3000 to get the JSON, instead you would use something like http://123.456.789.123:3000.
But, usually, this is not the way you would do it (with the port 3000). What you would additionally do is put (for example) Nginx in front of your Node.js app (see an example here) in order to serve your api from the standard HTTP port (80).
So, basically, you can't actually "run Node.js server in Ionic app" - the way you do it is run the Node.js app separate from Ionic and expose its functionality via a standardized API (usually these days REST is what you would want to achieve) which you then "consume" via Ionic's (well, to be exact, it's actually Angular's) $resource module.
Hope this helps clear things up a bit.
I'm trying to set up a small development environment consisting of a node.js http server, a mongodb database and frontend in angular.js. For development purposes I've created an account with MongoHQ and can reference my db using a URL.
The issue I'm faced with is that I can easily connect to my db from my Angular code but then my connection info is exposed through my http server.
So what I would like to be able to is to create my connection in my NodeJS server.js file and reference it from eg. my AngularJS app.js file. Is that possible and if so how?
Thanks in advance
Try using express and mongoose.
Server side code
var express = require('express');
var app = express();
//start server with port of choice here
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var Foo=mongoose.model('foo');
//foo is a model. Check mongoose documentation from collections and schemas
app.get('/hello', function(req, res){
//get data from mongo using mongoose
foo.find({},function(err,docs){
//do stuff here
res.send(docs)
})
});
Front end code
$http.get('/hello').success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
//data is the data from mongodb
})
Follow this tutorial to get an idea
There is a stack called MEAN Stack. See if it fits your needs. Very easy to set up and develop.
Hi is there a way to access the base http server context of sails? I want to use binaryJS in my app and In the gettig started guide they are talking about creating the server at your own, it you have an existing express app with the following line:
var server = http.createServer(app).listen(9000);
than I could use the following binaryJS command:
// Create a BinaryServer attached to our existing server
var binaryserver = new BinaryServer({server: server, path: '/binary-endpoint'});
thank you
In Sails v0.10.x, you can access the underlying Express server with:
sails.hooks.http.server
in v0.9.x, it's
sails.express.server