I want to pass React Component from server-side, which is written in node.js.
Code(server.js):
var ReactDOMServer = require('react-dom/server');
var ReactApp = require('./console/ReactApp');
app.get('*', function(req, res, next){
var reactHtml = ReactDOMServer.renderToString(<ReactApp />);
res.render('index',
{reactOutput: reactHtml});
});
console/ReactApp.js:
var React = require('react');
var ReactApp = React.createClass({
render: function() {
return <div>Hello World</div>;
}
});
module.exports = ReactApp;
In both files, '<>' generated SyntaxError when running node server.js
var reactHtml = ReactDOMServer.renderToString(<ReactApp />);
^
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/.../www.js:7:11)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
that's is JSX. You need to transpile it before use or run it with babel-node (the latter is NOT a recommended method).
Or you can use transpiled code like this:
var reactHtml = ReactDOMServer.renderToString(React.createElement(ReactApp));
See full api of React.createElement here
Related
When I try to use koa-route 3.2.0 example from their website, I got error message router.routes is not a function
const Koa = require('koa');
const router = require('koa-route');
const app = new Koa();
app.use(logger());
router.get('/users', (ctx, next) => {
ctx.response.body =`<h1>Hello!</h1>`;
});
app.use(router.routes())
.use(router.allowedMethods());
// don't listen to this port if the app is required from a test script
if (!module.parent) {
app.listen(1337);
console.log('listening on port: 1337');
}
I got error message:
app.use(router.routes())
^
TypeError: router.routes is not a function
at Object.<anonymous> (/Projects/shoucast-front-end-prototype/script/server.js:40:16)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3
When I try to change
const router = require('koa-route');
to
var router = require('koa-router')();
I got error message:
const router = require('koa-route')();
^
TypeError: require(...) is not a function
at Object.<anonymous> (/Projects/shoucast-front-end-prototype/script/server.js:2:36)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3
Looks like you've installed the newer version, available at https://github.com/alexmingoia/koa-router/tree/master
You need to use new Router() instead.
Also looks like you're mixing up koa-router with koa-route, which are two different packages.
const Koa = require('koa');
const Router = require('koa-router');
const app = new Koa();
const router = new Router();
router.get('/', function (ctx, next) {
// ctx.router available
});
app
.use(router.routes())
.use(router.allowedMethods());
I want to add logInPageTest.js in a file with the require field, but when I am adding it , it is throwing this error.
module.js:472
throw err;
^
Error: Cannot find module './test-cases/logInPageTest.js'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/app/PycharmProjects/AutomationAppTesting/automationTest/index.js:15:15)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
Hierarchy is as follows:
-AutomationAppTesting
--automaitonTest
---test-cases
----logInPageTest.js
---index.js
Here is my code in index.js file:
require('./test-cases/logInPageTest.js')
Code in logInPageTest.js file:
var wd = require('wd');
var assert = require('assert');
module.exports = function (browser, next) {
maximizeWindow(browser, next);
}
function maximizeWindow(browser, next) {
browser .windowHandle( function (err, handle) {
browser.maximize(handle);
})
}
So, How should I give path to solve the error.
I have my nodejs project, and I want to separate loggers from my route.js to have a clean code.
But I have the next problem, when I try to load my logger to the route.js file, it shows me the next error:
TypeError: loggers.getTicketLogger is not a function
at Object.<anonymous> (/home/ismael/projects/nodejs-ticketing/routes/ticket.js:3:23)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/home/ismael/projects/nodejs-ticketing/server.js:49:1)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:141:18)
at node.js:933:3
I have the next code inside my loggers dir:
loggers/ticket.js
var log4js = require('log4js');
module.exports = function(){
getTicketLogger = function(){
var logger = null;
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file(__dirname + '/../log/ticket.log'), 'ticket-log');
logger = log4js.getLogger('ticket-log');
logger.setLevel('DEBUG');
return logger;
}
};
And my route.js file, has the next head
routes/ticket.js
var Ticket = require('../models/ticket'),
loggers = require('../loggers/ticket.js'),
log = loggers.getTicketLogger();
module.exports = function(app){
}
What I'm doing wrong, and how can improve the code to separate logger from route.js file.
Thanks you.
loggers/ticket.js should return an object that has your getTicketLogger method.
var log4js = require('log4js');
module.exports = function() {
var getTicketLogger = function(){
var logger = null;
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file(__dirname + '/../log/ticket.log'), 'ticket-log');
logger = log4js.getLogger('ticket-log');
logger.setLevel('DEBUG');
return logger;
}
return { getTicketLogger: getTicketLogger };
};
Then in routes/ticket.js
var loggers = require('./../loggers/ticket.js')();
var log = loggers.getTicketLogger;
typeof loggers; // => object
typeof log; // => function
I am trying to set mixed Schema in ExpressJs I tried to use mongoose.Schema.Types.Mixed didnĀ“t worked and tried explicit as shown bellow but unfortunately it fails with same error:
"C:\Program Files (x86)\JetBrains\WebStorm 9.0.3\bin\runnerw.exe"
"C:\Program Files (x86)\nodejs\node.exe" app.js
c:\Users\User\Desktop\nodekid\node_modules\node-restful\lib\model.js:247
app.getDetail = app.get;
^ TypeError: Cannot read property 'get' of undefined
at Function.Model.register (c:\Users\User\Desktop\nodekid\node_modules\node-restful\lib\model.js:247:22)
at Object. (c:\Users\User\Desktop\nodekid\routes\api.js:10:8)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object. (c:\Users\User\Desktop\nodekid\app.js:18:17)
at Module._compile (module.js:456:26)
Process finished with exit code 8
/**
* Created by User on 3/8/2015.
*/
var restful = require("node-restful");
var mongoose = restful.mongoose;
var storeSchema = new mongoose.Schema({
name: String,
products: [new mongoose.Schema({
name : String,
description : String
})]
});
module.exports = restful.model("store", storeSchema);
/**
* Created by User on 3/8/2015.
*/
var express = require("express");
var router = express().Router;
var stores = require ("../models/store");
stores.methods(["get","post","put","delete"]);
stores.register(router,"stores");
module.exports = router;
New Error:
c:\Users\User\Desktop\nodekid\node_modules\node-restful\lib\model.js:228
app[key](prefix + '/:id([0-9a-fA-F]{0,24}$)?', handlerlist);
^ TypeError: Object function createApplication() { var app = function(req, res, next) {
app.handle(req, res, next); };
mixin(app, EventEmitter.prototype, false); mixin(app, proto,
false);
app.request = { proto: req, app: app }; app.response = {
proto: res, app: app }; app.init(); return app; } has no method 'get'
at Function.Model.registerRoutes (c:\Users\User\Desktop\nodekid\node_modules\node-restful\lib\model.js:228:17)
at Function.Model.register (c:\Users\User\Desktop\nodekid\node_modules\node-restful\lib\model.js:248:8)
at Object. (c:\Users\User\Desktop\nodekid\routes\api.js:10:8)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object. (c:\Users\User\Desktop\nodekid\app.js:18:17)
The error was in api.js in setting routing:
var router = express().Router;
Just Change it with:
var router = express.Router();
Here is the error which I get when I run nodejs app.js
module.js:340
throw err;
^
Error: Cannot find module 'share'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/quicksilver/project/master/app.js:3:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
I am repeatedly getting this error :
I have already done npm install -g share several times ,but it has been no use.
var express = require('express')
var sharejs = require('share')
var connect = require('connect')
var AWS = require('aws-sdk');
//var zmq = require('zmq');
var app = express();
app.set('view options', {layout: false});
var problem={};
var sharejs_server;
AWS.config.loadFromPath('./awsconfig.json');
locomotive.boot('.', app.settings.env, function(err, app) {
var options = {};
if (err) { throw err; }
options.db = app.locals.db;
sharejs_server = sharejs.server.attach(app, options);
app.listen(app.locals.port, app.locals.ip, function () {
var addr = this.address();
console.log('listening on %s:%d', addr.address, addr.port);
});
});