Not found error in Get /create action in Node.js

Not found error in Get /create action in Node.js



I am working on creating a simple bulletin board with mysql and node.js.



I am using an express-generator and I want to write in index.js via routing and create.ejs in views.js.



My create.ejs has a form that passes input values ​​to post.



However, if you access /create, a Not Found (404 Error) occurs.


/create



routes/index.js


var express = require('express');
var router = express.Router();

var mysql = require("mysql");

var client = mysql.createConnection(
user: "root",
password: "ps",
database: "mysqlTest"
)

router.get('/create', function(req, res, next)
res.render('create');
);

router.post('/create', function(req, res, next)
var body = req.body;

client.query("INSERT INTO products (name, modelnumber, series) VALUES (?, ?, ?)", [
body.name, body.modelnumber, body.series
], function()
res.redirect("/create");
);
);

router.get('/', function(req, res, next)
client.query("SELECT * FROM products;", function(err, result, fields)
if(err)
console.log("There is an error in the query statement.");

else
res.json(result);

);

);
module.exports = router;



app.js


var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded( extended: false ));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next)
next(createError(404));
);

// error handler
app.use(function(err, req, res, next) 500);
res.render('error');
);

module.exports = app;






It's not clear to me what HTTP method is failing for you. Which URL/method is getting the unexpected 404? Seeing the form might help us.

– Jacob
Sep 8 '18 at 0:53






Ah, I think some punctuation made things weird. Is it the GET /create that's failing?

– Jacob
Sep 8 '18 at 0:54


GET /create






That's right. I get a 404 error in Get /create

– yori
Sep 8 '18 at 10:32




1 Answer
1



Upon quick glance I found the following items that may be incorrect



First,


var indexRouter = require('./routes/index');



NodeJS by default uses a file named index.js by default, so you can just specify the line like so


var indexRouter = require('./routes/'); //This is a recommended practice.



Second, in your lines
router.get('/create'... and router.post('/create', ... you have a slash in the name.


router.get('/create'... and router.post('/create', ...



However, since you are wiring this to the route in the following line:


app.use('/', indexRouter);



You can get rid of the slashes in the create endpoint. It should look more like this:



router.get('create'... and router.post('create', ...


router.get('create'... and router.post('create', ...



I think this will do it.
If not, I can run it through a debugger if you provide a package.json.






/ is called a slash. `` is a backslash.

– Jacob
Sep 8 '18 at 1:03



/






Unfortunately, my problem is not solved.

– yori
Sep 8 '18 at 10:33



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế