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"
Related
I used express in a NodeJs project, and i want to be able to request my server with theses routes :
/dogs
/pinguin
/bear
/wolf
/cat
/rat
I use a regex for this (http://forbeslindesay.github.io/express-route-tester/) :
Express Route Tester
It works correctly with express route tester but it failed when i tried with NodeJS
My code :
var express = require('express');
var app = express()
app.get("\/(dogs|pinguin|bear|wolf|cat|rat)", function (req, res) {
res.send('dogs or pinguin');
});
error :
return new RegExp(path, flags);
^
SyntaxError: Invalid regular expression: /^\/(?(?:([^\/]+?))|pinguin|bear|wolf|cat|rat)\/?$/: Invalid group
at new RegExp (<anonymous>)
at pathtoRegexp (C:\Users\Corentin\node_modules\path-to-regexp\index.js:128:10)
at new Layer (C:\Users\Corentin\node_modules\express\lib\router\layer.js:45:17)
at Function.route (C:\Users\Corentin\node_modules\express\lib\router\index.js:500:15)
at Function.app.<computed> [as get] (C:\Users\Corentin\node_modules\express\lib\application.js:481:30)
at Object.<anonymous> (C:\Users\Corentin\Documents\mesProjets\mdm\mdm-api\routes.js:36:5)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
try with the below code
app.get(/^\/(dogs|pinguin|bear|wolf|cat|rat)/, function (req, res) {
res.send('dogs or pinguin');
});
I am still relatively new to NodeJS. Now I encounter this problem: I had a file called "./models/route" I renamed it by changing the filename in VS Code to: "./models/routes". I then changed that name in all of the scripts. I checked, doubledchecked, triplechecked and quadruplechecked that it has been changed everywhere and it has.
Now I get this error in the console:
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module './models/routes'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (C:\node-projects\wandelverhalen\controllers\routesController.js:4:16)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (C:\node-projects\wandelverhalen\index.js:11:26)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
[nodemon] app crashed - waiting for file changes before starting..
The error apparently is encountered in internal/modules/cjs/loader.js:638. So it does not point me to a script that I made or can change.
I really am at a loss...
Edit:
As Mosifa points out in his comment the error points to ./wandelverhalen/controllers/routesController.js and ./wandelverhalen/index.js.
Here are the scripts. I still can't find the problem.
index.js:
"use strict";
const express = require("express");
const mongoose = require("mongoose");
const app = express();
const Routes = require("./models/routes");
const homeController = require("./controllers/homeController");
// const errorController = require("./controllers/errorController");
const routesController = require("./controllers/routesController");
mongoose.connect("mongodb://localhost:27017/db_wandelen", {
useNewUrlParser: true
});
const db = mongoose.connection;
db.once("open", () => {
console.log("Connected to MongoDB");
});
app
.set("port", process.env.PORT || 3000)
.set("view engine", "ejs")
.use(
express.urlencoded({
extended: false
})
)
.use(express.json())
.use(express.static("static"))
.use(layouts)
.use((req, res, next) => {
console.log(`Request made to ${req.url}`);
next();
})
// .use(errorController.pageNotFoundError)
// .use(errorController.internalServerError)
.get("/", homeController.start)
.get("/kempen", homeController.kempen)
.get("/rivieren", homeController.rivieren)
.get("/oost_belgie", homeController.oost_belgie)
.listen(app.get("port"), () => {
console.log(`Server running at port ${app.get("port")}`);
});
routesController.js:
"use strict";
const mongoose = require("mongoose");
const Routes = require("./models/routes");
exports.getAllRoutes = (req, res, next) => {
Routes.find({}, (error, routes) => {
if (error) next(error);
req.data = routes;
next();
});
};
I am trying to use socket.io on one of a simple project but when I run the same on my local machine I am getting this bug. I am not able to figure out what is going wrong my index.js is as follows
const app = require("express")();
const http = require("http").Server(app);
const io = require("socket.io")(http);
const port = process.env.PORT || 8080;
app.get("/", function(req, res) {
res.render("index.ejs");
});
http.listen(port, function() {
console.log("Listening on *:" + port);
});
The full log is here
Desktop/chat/node_modules/ws/lib/websocket.js:347
...options
^^^
SyntaxError: Unexpected token ...
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
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 Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (Desktop/chat/node_modules/ws/index.js:3:19)
My node version is 8.0
This looks like it's probably the object spread operator. If you showed the actual line of code that causes the error, we could see for sure.
You will need at least node v8.6 to get support for the object spread operator by default. Starting with node v8.21, it could be enabled via command line flags.
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.
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());