As a part of angular.js course, i downloaded and installed node.js, inserted server.js file in main folder with following content :
var connect = require('connect');
connect.createServer(
connect.static("../angularjs")
).listen(5000);
and then tried to run server by cli, but im getting error in cli:
TypeError: Object function createServer() {
function app(req, res, next){ app.handle(req, res, next); }
merge(app, proto);
merge(app, EventEmitter.prototype);
app.route = '/';
app.stack = [];
return app;
} has no method 'static'
at Object.<anonymous> (C:\Program Files (x86)\nodejs\server.js:4:19)
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)
at startup (node.js:119:16)
at node.js:906:3
try:
var connect = require('connect');
var serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic('angularjs'));
app.listen(5000);
Should work for you!
EDIT:
With Connect 3.0 .static() is moved to a separate package called serve-static.
So you'll have to install that before running this code.
Edit: THis is for an older version of Node and therefore doesn't answer the question. See the comments below.
To use connect, you need to setup the environment, and then create the server
var connect = require('connect');
car app = connect();
app.static("../angularjs");
connect.createServer(app).listen(5000);
You may also be able to do it the brief way that you have by:
connect.createServer(
connect().static("../angularjs")
).listen(5000);
Related
Within my Azure App Service Node.js backend I cannot seem to get the Javascript async/await feature to run. I have changed the default version of Node.js within application settings and package.json to above 7.6. (Changed to 8.9.0)
I would like to use this feature within a custom Express router shown here:
var express = require('express'),
bodyParser = require('body-parser');
var router = express.Router();
router.get('/', function (req, res, next) {
res.status(200).send('GET: This is a test response!');
});
router.post('/:id', async function (req, res, next) {
var context = req.azureMobile;
var newLovedOne = req.body.lovedone;
var newTie = req.body.tie;
console.log('POST: newLovedOne ', newLovedOne);
console.log('POST: newTie ', newTie);
try {
await context.tables('Tie').insert(newTie);
await context.tables('LovedOne').insert(newLovedOne);
} catch (error) {
res.status(500).send('Insert failed!');
}
});
module.exports = router;
Attempting to start the app with the above router produces this:
Application has thrown an uncaught exception and is terminated:
SyntaxError: missing ) after argument list
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> (D:\home\site\wwwroot\app.js:12:20)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
To verify whether the Node.js version is correctly set, you can go to your root and open the iisnode.yml file. Make sure it has the following line with the correct version:
nodeProcessCommandLine: "D:\Program Files (x86)\nodejs\8.9.0\node.exe"
I want to implement an API with POST method, But when i am starting my server then Error occured:
3450:~/Desktop/koa/ctx$ node koa2.js
/home/358/Desktop/koa/ctx/koa2.js:9
router.post('/locations', async (ctx, next) =>{
^
SyntaxError: Unexpected token (
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
358#daffolap358-Latitude-3450:~/Desktop/koa/ctx$
Can anyone tell me where I am doing wrong ?
My code is :
server.js:
var Koa =require('koa');
var middleware =require('koa-router');
var logger =require('koa-logger');
var parser =require('koa-bodyparser');
const router = middleware();
const app = new Koa();
router.post('/locations', async (ctx, next) =>{
console.log("ctx");
});
app
.use(logger()) // Logs information.
.use(parser()) // Parses json body requests.
.use(router.routes()) // Assigns routes.
.use(router.allowedMethods())
app.listen(5050, () => console.log('Listening on port 5050.'));
export default app;
You are getting this error because your node version is less than 7.6.0.
It should be greater than 7.6 also Async/await support in Node 7.6 comes from updating V8, Chromium’s JavaScript engine, to version 5.5.
Check your Node version. It should be >= 7.6.0. Otherwise koa-2 and async-await pattern will not work.
I am new to nodeJS. I am trying to use different middlewares with connect middleware.
this is my code:
var connect = require('connect');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var app = connect()
.use(connect.bodyParser())
.use(connect.cookieParser('tobi is a cool ferret'))
.use(function(req, res){
console.log(req.cookies);
console.log(req.signedCookies);
res.end('hello\n');
}).listen(3000);
I have installed every middleware through npm.
I am getting this error while running this file.
/home/dipesh/Desktop/temp/temp.js:5
.use(connect.bodyParser())
^
TypeError: Object function createServer() {
function app(req, res, next){ app.handle(req, res, next); }
merge(app, proto);
merge(app, EventEmitter.prototype);
app.route = '/';
app.stack = [];
return app;
} has no method 'bodyParser'
at Object.<anonymous> (/home/dipesh/Desktop/temp/temp.js:5:14)
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)
at startup (node.js:119:16)
at node.js:929:3
Any suggestions?
.use(bodyParser())
not
.use(connect.bodyParser())
You have required body-parser, but then never used it.
You are essentially doing
var a = function(){};
var b = {};
b.a();
which is not correct because b has not 'a' property.
I' m trying socket.io Cut and pasting the exact code they provide I have an error ! Any ideas I'm running on windows8 and node v0.10.26 ?
var io = require('socket.io')(server);
^
TypeError: object is not a function
at Object.<anonymous> (c:\Users\david wilson\TravelShopOffers\app_socketio:3:30)
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)
at startup (node.js:119:16)
at node.js:902:3
Here's the code from their site :
In conjunction with Express
Starting with 3.0, express applications have become request handler functions that you pass to http or http Server instances. You need to pass the Server to socket.io, and not the express application function.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
io.on('connection', function(){ /* … */ });
server.listen(3000);
Will not be the best answer, as I was just discovering and experimenting on express/socket.io a few days ago.
Anyway I came up with the following ( interested in feedback ) :
var app = require('express')();
var server = app.listen(3000);
var io = require('socket.io').listen(server);
io.sockets.on('connection', function(socket)
{
socket.on('myEvent',function(){ /* … */ });
}
);
Hope this will help
var express=require('express')
var app=express();
console.log("Encoded ",express.urlencoded());
app.use(express.urlencoded());
The above code throws the following error :
[user#localhost nodejs]$ node program.js
/home/user/Desktop/nodejs/program.js:41
console.log("Encoded ",express.urlencoded());
^
TypeError: Object function createApplication() {
var app = function(req, res, next) {
app.handle(req, res, next);
};
mixin(app, proto);
mixin(app, EventEmitter.prototype);
app.request = { __proto__: req, app: app };
app.response = { __proto__: res, app: app };
app.init();
return app;
} has no method 'urlencoded'
at Object.<anonymous> (/home/user/Desktop/nodejs/program.js:41:32)
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)
at startup (node.js:119:16)
at node.js:902:3
Seems like a similar question here - express.js trouble with connect modules but I'm already having express3.0.0 checked using the suggestion listed here - Find the version of an installed npm package
I also read the Api docs here - http://expressjs.com/api.html and they list urlencoded()
Please help.
I would also like to point, that I also tried using bodyParser() but that too gave the same error of has no method
The express guide is a bit-outdated.
For others having the same problem, the solution is that those methods have moved to a new module body-parser
Sample Code
var express=require('express');
var app=express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded());