Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142533

Cannot post back calculation to server

$
0
0

The problem showing up when I click the button submit.The error coming out 404 not found and "Cannot POST/Index" in the website. Am I have logical problems on the code or problem that occur on the syntax

I'm trying to post the number back to the server for the calculation and doesn't know where is the error that make me could not doing Post Function. Please get the requirement file from me if the file that I uploaded is not completed.

app.component.html
In my HTML file do I need to add somethings for link the server.ts? Is there still any issue that I have to check on it?

<!doctype html>
<html lang="en">
<head>
  <Title>Calculator</Title>
  <meta charset="utf-8">
  <title>Myapp</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">

  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />

</head>
<body>


  <h1 align="center">Angular Calculator</h1>
  <div class="container">
    <div class="card">
      <div class="card-body">

        <form action="index" method="POST">
        <input type="number" name="num1" class="form-control" placeholder="Number">
        <input type="number" name="num2" class="form-control" placeholder="Number">
        <select ng-model="operator" name="operator">
          <option>+</option>
          <option>*</option>
          <option>-</option>
          <option>/</option>
          </select>
        <button type="submit">Submit</button>
      </form>
      <p>Calculation Of the number is :{{ result }} </p>

      </div>
  </div>
  </div>

</body>
</html>

server.ts file
This is the by default server file that generate by npm which I'm not sure the syntax of code any problems for my first testing of addition functions.

    import 'zone.js/dist/zone-node';
import 'reflect-metadata';
import * as bodyParser from 'body-parser';

import * as express from 'express';
import {join} from 'path';

// Express server
const app = express();

const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'dist/browser');

// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const {AppServerModuleNgFactory, LAZY_MODULE_MAP, ngExpressEngine, provideModuleMap} = require('./dist/server/main');

// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
app.engine('html', ngExpressEngine({
  bootstrap: AppServerModuleNgFactory,
  providers: [
    provideModuleMap(LAZY_MODULE_MAP)
  ]
}));

app.set('view engine', 'html');
app.set('views', DIST_FOLDER);


app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Example Express Rest API endpoints
// app.get('/api/**', (req, res) => { });
// Serve static files from /browser
app.get('*.*', express.static(DIST_FOLDER, {
  maxAge: '1y'
}));

// All regular routes use the Universal engine
app.get('*', (req, res) => {
  res.render('/app.component.html', { req });
});
app.get('*', (req, res) => {
  res.status(404).send('data requests are not supported');
});

//have body-parser is a piece of express middleware that reads a form's 
//input and stores it as a javascript object accessible through
//can not to include body-parser but not to get the raw request, your body and header are not in the root object of request parameter
app.post('/',(req, res) => {
    //var num1 = req.body.operator
    var result=req.body;
    console.log(req.body);
    var operator = req.body.operator
        if (operator == "+")
        {
            res.render('/app.component.html', {
                result: parseInt(req.body.num1) + parseInt(req.body.num2),
            });
        } 



})
// Start up the Node server
app.listen(PORT, () => {
  console.log(`Node Express server listening on http://localhost:${PORT}`);
});

Viewing all articles
Browse latest Browse all 142533

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>