AngularJS, nodeJS, ReferenceError: require is not defined - node.js

I have read several similar questions here on SO but couldn't find any answer that fits my case.
I am new to angularjs and nodejs, and I am stuck here:
I would like to store the input of a form into a json file. For that I found out about this procedure:
var fs = require('fs');
fs.appendFile('../database/lexicon.json', ' This is my text.', function (err) {
if (err) throw err;
console.log('Updated!');
});
I am just testing here with 'This is my text', and my json file is located here ../database/lexicon.json
But the issue comes from the fact that my program doesn't recognise the require.
I understand that it is a server side action and that it can not run in the browser, but I thought I would overcome that issue by running my app in a local server with this command:
npx http-server -o
Can anyone help me out with this issue? Thanks!

OK, I solved the problem, not with this example, but I moved on to another feature of my app that require another require and I don't get the error message anymore after installing express.js !
I hope this will help others.

Related

ExpressJS-Localhost not showing anything

This is my first time I am running ExpressJS. But the Localhost is not responding any data.
Below is the Code I have written
const express=require('express');
const app=express();
app.get('/',(req,res)=>
{
});
app.listen(3000,()=>console.log("running at 3000 port"));
I have tried to change the localhost to 4000,5000.But same error.
Additional Information:
Running on Ubuntu 16.10
Any clue of whats happening over here?
You're not giving the browser anything to render. Try following the Hello World example in the docs and you might be pleasantly surprised:
https://expressjs.com/en/starter/hello-world.html
I dont know if you still need an answer but i think i might have it.
I had the same issue with the 'app.use(express.json)'
when i deleted my server started working, it was because we were writing it work it actually is
'app.use(express.json())'
PD: that command works for telling EXPRESS that it has to read a json file or request...
i hope this can help you out
Just to add to imjared's answer.. Always, response has to be sent back to the client(Browser) for any new page you create because that's what will be displayed on the browser. Adding res.send("some data") in the function will resolve the issue.

Azure Functions crashing loading MongoDB in Node

I'm attempting to write an Azure Function, in Node, to connect into a MongoDB instance (Cosmos DB in this case).
However, as soon I run require("mongodb"), my function crashes, without throwing an error, or logging anything, with the HTTP response returning a 502 code.
My setup:
Creating a function app using all defaults through the Azure portal.
Creating a package.json with mongodb version 3.x.
Running npm install through the Kudu shell
Include the require statement in my code.
Make a request to the function
This doesn't throw an error in the code, and I see logging that's run before, but not after the require statement (which is making it pretty difficult to debug).
I've also tried following through this guide about running a mongo query from a function, and it fails in exactly the same way for me.
After putting some hooks into Node's module module, my attempts to debug this led to a line in one of mongo's dependencies that fails in a similar way when run in isolation (from saslprep), which seems to stem from running out of stack space.
However, this feels like its a pretty mainstream use for an Azure function, and I haven't seen any similar issues, so I'm inclined to suspect that its an issue with my setup, rather than the mongodb library, but I haven't been able to find a misconfiguration, as I haven't changed any defaults - right now, I'm stumped!
I don't have a full code example right now, as I'm away from my work computer, but the code is something like
const mongo = require('mongodb');
module.exports = function(context) {
context.res = {
body: 'Hello world'
};
context.done();
}
Without the require statement, the code runs fine, returning the response to the browser.
It turns out that this problem was caused by running out of stack space. After pushing a patch to the saslprep library (v1.0.1), this has now been fixed.
Im pretty sure that if you add to your require function the same as in Microsofts Cosmos DB guides for mongo the following it should work
var mongodb = require('mongodb').MongoClient;
you have it as:
const mongodb = require('mongodb');
Im curious to know if that makes a difference. After looking through Microsofts own docs nearly all of them are declared that way.
Here is the tutorial I found: MongoDB app using Node.js

How to use the pg node package in angular

Situation
Hi, I'm quite new to Angular, I've been doing some projects following tutorials, which then lead me to try to start my own project to practice my Postgres and newly acquired Angular "skills".
I am trying to do a webapp that connects to a postgres DB using the node pg module.
(I know sequelize is a thing and it seems to work better than pg but AFAIK sequelize doesn't let you run pure postgres commands through it) Please correct me if I am wrong about this
The problem
This is where I get stuck, I am trying to follow the instructions from the docs but it doesn't seem to work correctly.
I have tried:
const { Client } = require('pg');
import { Client } from 'pg';
Also tried importing it in the .angular-cli.json in the scripts array
All of these fail with errors similar to this
ERROR in ./node_modules/pg/lib/connection-parameters.js Module not found: Error: Can't resolve 'dns' in '[...]\node_modules\pg\lib'
ERROR in ./node_modules/pg/lib/native/client.js Module not found: Error: Can't resolve 'pg-native' in '[...]\node_modules\pg\lib\native'
But nothing seems to work properly. Am I doing this completely wrong?
Also, pretty dumb question. I believe angular does everything on the client side, this is a HUGE security risk for DB access in prod. If that is true, is there a way to write server-side .ts services? or are services server-side?
You could write your serverside code in node using compiled ts, but probably not with angular.

Calling server-side code from client on Derby.js

I'm new to using Derby.js, and have scaffolded out a project using the yeoman generator-derby package. I thought everything was going fine, but I can't figure out what I'm doing wrong here.
A breakdown:
I have an 'app/dbWp.js' controller that exports several functions, and requires the modules 'mysql', 'async', and 'needle'
In my app/index.js, I import this file and use it like so:
app.proto.submitWp = function() {
dbWp.createUser(this.model);
};
I call this function from the view/index.jade like so:
button.btn.btn-primary(type="button", on-click="submitWp()")
In the browser, I get numerous console.error message complaining about the 'fs' module not being defined. After much googling, I discover that it's due to Browserify ignoring the 'fs' module, which subsequently causes problems with modules 'mysql' and 'needle'. But that implies this code is being executed in the browser?
So my question is: why is this trying to call the function on the client side? Obviously if it executes on the server side, as I thought it was going to, there wouldn't be a problem requiring these modules.
How can I execute this function on the server? Had this working fine with express + socket.io before, but wanted to change frameworks and give Derby.js a shot.
I'm clearly misunderstanding something about how Derby.js is supposed to work; any help would be appreciated. Thanks!
I know this is like 4 months later, but being new to DerbyJs too, I thought I could try and help.
I personally with standard html code have the equivalent working.
<button on-click="editContact(#contact.id)">Edit Contact</button>
This indeed runs code on the server. Can you try writing your code in standard HTML, or perhaps better yet, see if you can do a console.log on the server method to see if it even is getting there?
Perhaps the best would be to call an empty function on the server with a console log and check both the browser console and the server console.

Async profiling nodejs server to review the code?

We encountered performance problem on our nodejs server holding 100k ip everyday.
Now we want to review the code and find the bottle-neck.
#jfriend00 from what we can see now, the problem seems to be DB access and file access. But we don't know what logic caused this access.
We are still looking for good ways to do the async profiling of nodejs server.
Here's what we tried
Nodetime
This works for us to some extent. It can give the executing time of code specified to the lines. However, we can't locate the error because the server works async and no stacking and calling info can be determined.
Async-profiling
This works with async and is said to be the first of this kind.
Problem is, we've integrated it's js code with our server-side code.
var AsyncProfile = require('async-profile')
AsyncProfile.profile(function () {
///// OUR SERVER-SIDE CODE RESIDES HERE
setTimeout(function () {
// doAsyncStuff
});
});
We can only record the profile of one time of server execution for one request. Can we use this code with things like forever? I've no idea with this.
dtrace
This is too general for us to locate problem in nodejs code.
Do you have any idea on profiling nodejs server code? Any hints or suggestions are appreciated. Thanks.

Resources