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.
Related
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.
I have two separate node.js express servers running on different ports.
on port 5000 is running an Authentication API that handles the register, login, and session verification.
on port 6000 is running a Product API that handles the CRUD operations for the products.
When I create a new product I would like to verify the token found in the request header, so instead of copying over the session verification method from the Authorization API, I imported it, but for some reason, I get this error in the console when starting the app: Error: listen EADDRINUSE: address already in use :::5000
Authorization API exports the method:
export const verifySessionToken = async (sToken: string) => { ... }
Products API imports the Method:
import { verifySessionToken } from '../../../auth/common/verify-session';
If I comment out the import from above, the app runs again.
Is it even possible to import methods from node apps running on different ports?
If it is, what would be the correct way of doing it?
Million thanks!
First off, you import functions from modules. You don't import methods from servers. And, it's perfectly feasible to import the same function for use in two separate servers either in the same nodejs process or in different nodejs processes. The process of importing something from a module has absolutely nothing to do with a server or a port that server is running on. You're just importing a function reference from a file that you can call later.
You do need to make sure that your code is properly modularized so that the process of importing the function doesn't have any unintended side effects such as trying to start another server that you don't want to start. So, perhaps your function isn't properly modularized (put in its own sharable module)?
Is it even possible to import methods from node apps running on different ports? If it is, what would be the correct way of doing it?
Yes. It's very easy if you create your module properly and make sure that it doesn't have any unintended side effects. If you show us the entire module that you're importing from, we can probably help you identify what you are not doing correctly.
FYI, just put this:
export const verifySessionToken = async (sToken: string) => { ... }
in its own file where both places that want to use it can then import it.
I don't think you can run two servers sharing the same files. Why don't you just replicate your function in the other app ?
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
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.
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.