browserify Wordnet thesaurus - javascript

I am using a thesaurus API (altervista) for my JavaScript web app but I want to be able to make lots of synonym requests without worrying about API quotas, etc. I want to self-host a thesaurus on my web host and I would like to send words and receive their synonyms from JavaScript in the browser.
As research I tried node, and within node I was able to get synonyms with these packages:
"natural" and "wordnet-magic"
so then I tried to browserify "natural" and "wordnet-magic" node packages. On attempting to browserify "natural":
"Error: Cannot find module 'lapack'"
"lapack seems to be a native OS-dependent shared library, so it can't be browserified." https://github.com/moos/wordpos/issues/9
Also I had no luck browserifying "wordnet-magic":
"Uncaught TypeError: Cannot read property '_ansicursor' of undefined"
Possibly related (since sqlite3 is in my wordnet-magic packages), instances of same error reported here but still unresolved: https://github.com/mapbox/node-sqlite3/issues/512
My second choice would be a PHP solution should it be impossible in JavaScript. It does not have to use Browserify or Wordnet, but Wordnet would be such an amazing thing to have in the browser. Thanks.

Okay I can get synonyms in the browser (thanks to Stuart Watt):
I followed instructions to setup a javascript wordnet app here:
https://github.com/morungos/wordnet
then did
npm install express
and then ran this code with node:
var express = require('express');
var app = express();
var WordNet = require('node-wordnet');
var wordnet = new WordNet();
app.get('/lookup', function(req, res) {
wordnet.lookup(req.query.word, function(results) {
res.send(results);
});
});
app.listen(3000, function() {
console.log('Example app listening on port 3000!');
});
and you can then see wordnet in your browser, e.g.
http://localhost:3000/lookup?word=wind
It's visible, it works, and to consume it in your html, see this answer:
https://stackoverflow.com/a/36526208/5350539

Related

Can't get 'var express = require("express")' to work

I am new to js and I am trying to develop a simple node.js-mysql app. No matter what I do I can't get the standard
var express = require("express");
statement to work.
I have installed node.js and express correctly, express is in package.json. I have a local server running. But this simple line will not work.
On the node.js side at Windows command line I have no error but when I go to localhost:3000 on the browser, I get
'Uncaught Error: Module name "express" has not been loaded yet for
context: _. Use require([])' error at js console.
I tried changing it to
require(['express']`, function (express) {}
as suggested at node.js web site but then at the Windows command terminal I get a different error saying like
'expecting a string but received an array....'.
I have tried import instead of require and I have tried every suggestion that I could find on the Internet. I have been blowing my brains for weeks to get this to work with no success. I am so frustrated that I am seriously thinking about giving up all together. If someone can help I will be forever greatfull to him/her.
My main js code is as follows:
var port = 3000;
// Import or load node.js dependency modules.
var express = require("express");
var app = express();
var path = require("path");
var bodyParser = require("body-parser");
app.use(express.urlencoded({ extended: true })); // to support URL-encoded bodies.
app.listen(port, () => {
console.log(`server is running at http://127.0.0.1:8887`);
});
app.get('/', function(req, res){
res.sendFile("D:/Behran's files/Web projects/havuzlusite/index.html");
});
Require.JS is for loading AMD modules (and is, honestly, obsolete in today's JS landscape).
Node.js modules are either ECMAScript modules (which use import and export) or CommonJS modules (which use require and module.exports).
Even though both AMD and CommonJS modules use a function named require they are not compatible.
There are methods you can use to run ES modules and CommonJS modules in the browser however they can't replace APIs that are provided by runtimes.
Express.js needs to be able to listen for incoming HTTP requests. Browsers do not provide any mechanism to make that possible. Node.js does.
If you want to run Express.js you have to run it using Node.js and not a browser.
Express.js creates an HTTP server. A browser can make requests to it (e.g. if you type http://127.0.0.1:3000 into the address bar.
(Your code says server is running at http://127.0.0.1:8887 but the port constant is set to 3000).
All your Express.js code must run through Node.js.
You can't send a copy of that code to the browser and run it there too.

Using a node.js app with HTML

My goal is this: JS but server-side. My solution, the obvious, node.js. I've used node.js quiet a bit. Mainly for an application, not a web server. The only reason I need to do server-side JS is that I need to use a library that connects to the Discord API. So I have a little test .js file with my node.js in it. It just prints text if it works. Basic. What I need it to do is whenever someone goes to https://example.com/something, it runs the node.js script and if the script ends up with printing "hello", then https://example.com/something will say "hello".
I've done some research on this, I've found ways to deploy a node.js app, which I know how to do. I can't really find anything that I'm looking for though.
You can use express to run a webserver on nodejs
Install express by running "npm install express" in your project folder through command prompt
Create a app.js file with the following code
var express = require('express'); // load the express library
var app = express(); // create an instance of express
var child_process = require('child_process'); //load the child_process module
app.get("/something", function(req, res) { // Setup a router which listens to the site http://localhost/something
child_process.fork("./yourCodeFile.js"); // Launch your code file
});
app.listen(80);
Run node app.js to listen to web connections
Then you put your code into the yourCodeFile.js which has to be be in the same folder as the app.js file, even better you could just write all your code in the app.js code as long as you keep it inside the function inside app.get
You should take a look at cloud-based lambda functions and platforms like AWS Lambda, which run a script in response to an HTTP request. They are relatively new and the architecture used to support this is being called "serverless", which is a simple term, albeit a bit of a misnomer. There are various tools out there to help you build these systems, such as the similarly named Serverless framework, though you can typically still use more traditional server frameworks that you are probably more comfortable with. Either way, you are not responsible for managing any server, including starting it or stopping it.
In terms of constructing a response that you are happy with, you can of course respond with any arbitrary string you want. See the AWS example of a Node.js handler.
exports.myHandler = function(event, context, callback) {
callback(null, "Hello, world!");
}
Lambda functions can also return binary data and work well with static storage systems like Amazon S3. For example, the function can be run in response to the creation of static assets.
Your code should look like this:
const http = require('http');
const url = require('url');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
const pathName =url.parse(req.url).pathname;
if (pathName == '/something') {
res.end('Hello World\n');
} else {
res.end('Please visit /something \n');
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
You should run your file with node youfile.js And when you do curl http://127.0.0.1:3000 you will see
Please visit /something
But when you do curl http://127.0.0.1:3000/something you will see
Hello World

What is the easiest way to spin up a node/js app?

I am having trouble building a node/js app with various frameworks. I keep getting the error require is not defined even though I have followed various browserify tutorials to fix it.
To give a flavour of what I want to do. I want the app to be able to be ran on a server and then I can npm install anything and these things work smoothly. I have been using express, firebase etc to handle some of my issues.
This is an extract of my app.js file:
var express = require('express');
var app = express();
var firebase = require('firebase');
app.get('/', function (req, res) {
res.sendFile(__dirname + '/html/index.html');
});
app.get('/welcome', function (req, res) {
res.sendFile(__dirname + '/html/welcome.html');
});
I have tried to use bundle.js to get require working but it still insists it is not defined.
Browserify will let you transpile some code so it will run in the browser.
It won't let you do things which are fundamentally impossible in the browser (such as running an HTTP server as you are trying to do here).
If you want to run code that requires Node JS then you need to run it through Node JS. Typically via node app.js in your command line shell.

how to run node.js on windows with apache server installed in?

I'm a node.js begginer . Let's say I have an apache server(XAAMP) and node.js installed in C:\Program Files\nodejs\nodejs.exe on windows 7.
How can I run node.js in my apache server to simulate my code?
I mean, I know how to write node.js code but what I don't know how it's work on my server?
Apache server don't need for Node.js.
For create your own Node.js server:
Download and install Node.js
Create file hello.js:
var http = require("http");
var server = http.createServer().listen(3000); // beter way for create
server.on("request", function(req, res){
res.writeHead(200, {"Content-Type": "text/plain"});
// for view at page http://localhost:3000
res.write("Hello world");
res.end();
});
server.on("listening", function(){
// for view in console
console.log("Listen: 3000...");
});
In terminal go to dir where file hello.js and type:
node hello.js
Open your browser and point it at http://localhost:3000/. This should display a web page that says:
Hello world
A basic HTTP server
Node.js Manual & Documentation
If you like to work with a replacement for XAAMP you should finally take a look at MEAN.io.
At NpmJS.org you will find different solutions for most of your needs.
and like Reagan Gallant commented you should take a look at this famous stackoverflow post (if you need ideas).
NodeSchool indeed is a good entry point for your fist steps. After that npmjs will make sense and finally you will love Mean.io
You just make it use a different port than Apache is using (for example port 3000 which is the default for express-js and others) -- that is assuming that you don't need the two to work together.
If you do need them to work together, you add a forwarding module to Apache and configure the forwarding in Apache of certain URL to go to your local port for node-js

Where is the Socket.IO client-side .js file located?

I am trying to get socket.io (Node library) to work.
I have the server-side js working, and it is listening. The socket.io website states simply:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
This is nice, however, what JS file am I importing!?!
I went into the node_modules directory, where I installed socket.io through npm, and inside socket.io/lib/ is socket.io.js file. However, this is server-side (uses the phrase require(), which errors on the client).
I have spent an hour looking around and I can't get any client .js file to work.
What am I missing?
I managed to eventually answer this for myself.
The socket.io getting started page isn't clear on this, but I found that the server side of socket.io automatically hosts the .js file on starting node, in the directory specified in the documentation:
"/socket.io/socket.io.js"
So you literally just point to this url regardless of your web app structure, and it works.
I would suggest checking if your node_modules directory is at the top level of your app directory. Also, I do believe you need to specify a port number; you should write something like var socket = io.connect('http://localhost:1337');, where the port number is 1337.
If you did npm install then the client socket.io file is located at node_modules/socket.io-client/dist/socket.io.js
Source: Socket get-started page
The client is available in a few ways:
supplied by the socket.io server at /socket.io/socket.io.js
included via webpack as the module socket.io-client
via the official CDN https://cdnjs.cloudflare.com/ajax/libs/socket.io/<version>/socket.io.js
For the first one, the server can be configured in a couple of ways:
// standalone
var io = require('socket.io')(port);
// with existing server from e.g. http.createServer or app.listen
var io = require('socket.io')(server);

Categories

Resources